Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    56
    Thanks
    5
    Thanked 2 Times in 2 Posts

    fwrite and functions

    I have a large function with many variables and would like to use fwrite to save the output to a file.

    In this simple function example below what would be the proper way to pass the function writeMsg to a .txt or .php file on the server?

    PHP Code:
    <?php
    function writeMsg() {
       echo 
    "Hello world!";
    }

    writeMsg();
    ?>

    thanks

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Just use file_put_contents() if all you need to do is log some data. It creates the file, truncates it, then writes what you want it to.

    If you're looking for more, then use fwrite().

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    56
    Thanks
    5
    Thanked 2 Times in 2 Posts
    thanks, I tried both I guess Im having trouble nailing down the correct syntax to pass a function to a $variable

  • #4
    New Coder
    Join Date
    Feb 2011
    Posts
    56
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Just an update, I am able to get the contents of my php file to write to a txt doc but what I am trying to do is gather the output from the PHP not the php code it's self.

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Your question is unclear to me now, first you say you want to write data to a file, now you're saying you want to save the output. I'm lost now...

    What are you trying to do with this exactly? Are you trying to display text in an SSH window or print text to a webpage? Either way, echo should do this for you.

  • #6
    New Coder
    Join Date
    Feb 2011
    Posts
    56
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Hi Dubz, thx for chiming in.. Yes, I am trying to write the data that is created from my function to a file. Sorry for not being clear from the get go..

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    So you want to log the echo'd data to a text file? Why not just use an SSH client to read the data or use file_put_contents() instead of echo?

    Otherwise, look into output buffering. use ob_start(); to start buffering and then use one of the listed functions to do as you desire.

  • #8
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,325
    Thanks
    23
    Thanked 616 Times in 615 Posts
    @neptune1,
    To beat a dead horse and maybe make things simpler for you:
    PHP Code:
    <?php
    function writeMsg() {
        
    $my_var "Hello world!";  // Make a variable
        
    echo $my_var;

        
    $my_file 'file.txt';  // The name of your file
        
    $handle fopen($my_file'w') or die('Cannot open file:  '.$my_file); // creates file

         
    fwrite($handle$my_var);  // write to file

        
    fclose($handle);  // close the file
    }

    writeMsg();
    ?>
    You will find all the php function about file handling here PHP: Filesystem - Manual
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #9
    New Coder
    Join Date
    Feb 2011
    Posts
    56
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Thanks sunfighter, my code is similar and working. If the function writeMsg had a handful of if/else aguments in it how can fwrite or file_put_contents() be used to write the output from said function.

  • #10
    New Coder
    Join Date
    Feb 2011
    Posts
    56
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Dubz View Post
    So you want to log the echo'd data to a text file? Why not just use an SSH client to read the data or use file_put_contents() instead of echo?

    Otherwise, look into output buffering. use ob_start(); to start buffering and then use one of the listed functions to do as you desire.
    Thanks, I did't even know you could file_put_contents() instead of echo

    I'll check out ob as well

  • #11
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,325
    Thanks
    23
    Thanked 616 Times in 615 Posts
    @neptune1,
    snip...If the function writeMsg had a handful of if/else aguments in it how can fwrite or file_put_contents() be used to write the output from said function.
    Yes. I'm guessing at what you mean, but it is yes even if this is wrong:
    PHP Code:
    <?php

    function writeMsg($mess){
        if(
    $mess == "Sam"){
            
    $my_var "Hello world!";  // Make a variable
        
    }else if($mess == "Barnum"){
            
    $my_var "It just don't matter nowe";
        }else{
            
    $my_var "A sucker born every minute";
        }
    echo 
    $my_var;

        
    $my_file 'file.txt';
        
    $handle fopen($my_file'w') or die('Cannot open file:  '.$my_file); // creates file

         
    fwrite($handle$my_var);  // write to file

        
    fclose($handle);  // close the file
    }
    writeMsg("Julie");
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #12
    New Coder
    Join Date
    Feb 2011
    Posts
    56
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Thanks I totally got it. my function had 20 elseif's and I was trying to think of a way to not have to update all of the echo statements and somehow pass the output from a cron job refresh to a file.

    I got it to work with file_put_contents() and it works great. My program pulls data from a noaa rss feed and then it checks wind, wave height, swell period and wave direction and generates an accurate surf report. Thanks for the feedback and sorry for the convoluted question


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •