Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP writing lots of text to STDERR

When writing a large chunk to STDOUT in PHP you can do this:

echo <<<END_OF_STUFF
lots and lots of text
over multiple lines
etc.etc
END_OF_STUFF;

(i.e. heredoc)

I have the need to do a similar thing but to STDERR. Is there another command like echo but uses STDERR instead?

like image 963
Ed Heal Avatar asked Aug 20 '12 14:08

Ed Heal


3 Answers

Yes, using php:// stream wrapper: http://php.net/manual/en/wrappers.php.php

$stuff = <<<END_OF_STUFF
lots and lots of text
over multiple lines
etc.etc
END_OF_STUFF;

$fh = fopen('php://stderr','a'); //both (a)ppending, and (w)riting will work
fwrite($fh,$stuff);
fclose($fh);
like image 128
Mchl Avatar answered Nov 11 '22 06:11

Mchl


For a simple solution - try this

file_put_contents('php://stderr', 'This text goes to STDERR',FILE_APPEND);

The FILE_APPEND parameter will append data and not overwrite it. You could also write directly to the error stream using the fopen and fwrite functions.

More info can be found at - http://php.net/manual/en/features.commandline.io-streams.php

like image 36
Lix Avatar answered Nov 11 '22 06:11

Lix


In the CLI SAPI, it can be as simple as passing a Heredoc string as an argument to fwrite() with the STDERR constant.

fwrite(STDERR, <<< EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD
);
like image 2
Keith Shaw Avatar answered Nov 11 '22 08:11

Keith Shaw