Daniel Rench

Web application development : Servers : Networks : E-Mail : DNS : Databases : Programming for hire

previous : contact : linkedin : code : links : pictures : facebook : twitter : next

Because nobody has ever put the phrase "print to STDERR in PHP" on a web page

I needed to know how to do this, and this particular phrase (quoted) returns zero documents on well-known search engines. See, PHP does define STDERR (and STDOUT and STDIN) for use in print() statements, but (this is funny) not when running under a web server. So in order to print debugging information to your server's error log, you have to open it yourself and use fwrite():

	$STDERR = fopen('php://stderr', 'w+');
	fwrite($STDERR, "some debug info\n");

Printing to standard error is the #1 most common debug aid in the universe of all time (almost 6000 years). Yet PHP goes out of its way to take it away from you. It may be there for PHP scripts not run under a web server, but that's almost the same as saying it's not there at all.

It also sounds like maybe at one time STDERR, etc. worked as programmers would expect but the last word on that was "Not PHP problem -> bogus."

Dec 11, 2006 update: Thanks to reader Andy Bakun I now know about PHP's error_log() function. When called with one argument (a message string), it sends the string to the web server's error log (when running under a web server), or to STDERR (when running from the command line). So I think it's safe to say error_log() is PHP's do-what-I-mean equivalent of writing to STDERR in any other language. And now it's documented somewhere.

<< Excitement Has a New Name: Excessively Strong Typing in Perl | Home | Portfolio | Contact | Excitement Has A New Name: Tmetic::Feed::Manager >>