How to use STDOUT in php
Okay, let me give you another example for the STDIN
and STDOUT
usage.
In PHP you use these two idioms:
$input = fgets(STDIN);
fwrite(STDOUT, $output);
When from the commandline you utilize them as such:
cat "input.txt" | php script.php > "output.txt"
php script.php < input.txt > output.txt
echo "input..." | php script.php | sort | tee output.txt
That's all these things do. Piping in, or piping out. And the incoming parts will appear in STDIN
, whereas your output should go to STDOUT
. Never cross the streams, folks!
The constants STDIN
and STDOUT
are already resources, so all you need to do is
fwrite(STDOUT, 'foo');
See http://php.net/manual/en/wrappers.php
php://stdin, php://stdout and php://stderr allow direct access to the corresponding input or output stream of the PHP process. The stream references a duplicate file descriptor, so if you open php://stdin and later close it, you close only your copy of the descriptor-the actual stream referenced by STDIN is unaffected. Note that PHP exhibited buggy behavior in this regard until PHP 5.2.1. It is recommended that you simply use the constants STDIN, STDOUT and STDERR instead of manually opening streams using these wrappers.
this should work for you
$out = fopen('php://output', 'w'); //output handler
fputs($out, "your output string.\n"); //writing output operation
fclose($out); //closing handler