I'm using PHP at the command line to write a bunch of SQL commands out. If an error arose I wanted it to go to STDERR instead of being printed out and messing up my SQL. PHP, being a web based language, doesn't make this real obvious. The solution ended up being error_log(), which if called from the CLI sends a message to STDERR.
error_log("This message will go to STDERR");
In web context error_log() logs to Apache's error_log, where ever that is mapped.