redirect stdout to stderr

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



How can I implement in PHP, a script which redirects stdout to stderr,
such that echo, etc. print to stderr instead of stdout?

I can redirect stdout to stderr when invoking PHP like so:

php script-name >&2

However I want to perform this redirection within the script itself.

The solution I currently use is output buffering:

ob_start();

// Call library code

fwrite(STDERR, ob_get_contents());
ob_end_clean();

However I wonder if there's a more efficient way, so that output appears
on stderr immediately, rather than waiting for fwrite(STDERR,
ob_get_contents());

My reason for wanting this is to create a Subversion pre-commit hook
using PHP_CodeSniffer: http://pear.php.net/package/PHP_CodeSniffer

I want:

1) Commits to our Subversion repository to be checked against our coding
standard with PHP_CodeSniffer
2) Commits to fail when PHP_CodeSniffer returns an error
3) PHP_CodeSniffer's report to be displayed to the Subversion user, so
they can fix any problems

I achieved 1) and 2), but PHP_CodeSniffer prints its report to stdout
and Subversion only displays stderr to the user, not stdout. So to make
this pre-commit hook fool proof, I want it to redirect PHP_CodeSniffer's
report to stderr.

Anyone have better suggestions than output buffering?

Much thanks, Jack

Attachment: signature.asc
Description: This is a digitally signed message part


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux