将STDOUT中的PHP通知和警告重定向到STDERR

Is there a flag or a command available to redirect all PHP notices and warnings to STDERR on a single command you run on the command line?

I want to parse a JSON array from the command line, but sometimes I get notices and warnings from STDOUT which fail the JSON parsing.

The command looks like this:

rake collect

It's a rake task to collect some information about local websites.

You will need to create a custom error handler.

set_error_handler (function ($errno, $errstr, $errfile, $errline)
{
        switch ($errno) {
              case E_USER_ERROR:
                   $ERROR = "CUSTOM ERROR";
                   break;
              //set cases for each error type
        }
        if($ERROR){
        $stderr = fopen('php://stderr', 'w');
        fwrite($stderr, $ERROR );
        fclose($stderr);
    }
});

According to configuration manual, you need to set display_errors option to stderr in order to output warnings/errors to STDERR.

display_errors string

This determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user.

Value "stderr" sends the errors to stderr instead of stdout.

You can set this option in a few ways:

1) Globally (in php.ini)

display_errors = stderr

2) Locally in a script with ini_set():

ini_set('display_errors', 'stderr');

3) As a command line argument (without modifying any file)

php --define display_errors=stderr script.php