PHP gets an output stream or just an echo

What will be the difference in php command line application only for echo or printf etc. some kind of string, not to get the sdtout stream and write to it i.e.

$stdout = fopen('php://stdout', 'w'); 
+4
source share
4 answers

The first thing that comes to my mind is output buffering. echo and print with output buffering mechanism, but writing directly to stdout bypasses it.

Consider this script:

 <?php $stdout = fopen('php://stdout', 'w'); ob_start(); echo "echo output\n"; fwrite($stdout, "FWRITTEN\n"); echo "Also echo\n"; $out = ob_get_clean(); echo $out; 

which outputs:

 FWRITTEN echo output Also echo 

which demonstrates that echo buffered, while fwrite is not.

+5
source

The difference is that echo writes to php://output , which is the stream of the output buffer. However, php://stdout gives you direct access to a process output stream that is not loaded.

For more information on streams, see the manual: http://www.php.net/manual/en/wrappers.php.php

+2
source

In addition to the technical difference already noted, there is also an obvious difference in style and consensus. echo / print is the normal way to create output in PHP; writing to output as if the file was abnormal and potentially confusing. For the sake of clarity of your intentions, I would not advise you to do this if you have no good reason for this.

0
source

Here php://stdout not a buffered stream, fwrite can buffer output with php://output

0
source

All Articles