Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP output to command line

Tags:

php

I start my script from command line and it outputs things as they happen but a week ago it stopped outputing and now outputs everything when script finishes. I have ob_start() but as I know this does not effect command line output.

like image 826
dfilkovi Avatar asked Dec 11 '09 17:12

dfilkovi


2 Answers

You need to remove ob_start()... try this code on the command line, and it will print the text all at once:

<?
ob_start();
echo "test\n";
sleep(10);
echo "buffer\n";
?>
like image 109
r00fus Avatar answered Oct 03 '22 09:10

r00fus


An easy way to do it is to create a function in php like this:

function console_log($message) {
    $STDERR = fopen("php://stderr", "w");
              fwrite($STDERR, "\n".$message."\n\n");
              fclose($STDERR);
}

where $message is the desired output to command line. Then simply call the function wherever you would like to output and pass in whatever you want it to print.

like image 45
ghoozie Avatar answered Oct 03 '22 10:10

ghoozie