PHP writing lots of text to STDERR

Ed Heal picture Ed Heal · Aug 20, 2012 · Viewed 11k times · Source

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?

Answer

Mchl picture Mchl · Aug 20, 2012

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);