PHP write stderr - Write to STDERR in a PHP script

PHP STDERR FAQ - How do I write to STDERR in a PHP script, specifically a PHP command line script?

I've written a lot of my command line scripts in PHP lately, and I just ran across this problem in my own script, how to write to STDERR in PHP. Fortunately the solution is easy, just use the PHP fwrite function like this:

fwrite(STDERR, “hello, world\n”);

Or, in a more real-world example, here's how I'm writing MySQL error messages to STDERR in PHP:

fwrite(STDERR, "MySQL INSERT error on \"INSERT INTO files\" statement: " . mysql_error());

I hope this PHP write to STDERR FAQ has been helpful. As usual, if you know of a better way, just leave a comment below.