Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

stdout - PHP CLI doesn't use stderr to output errors

I'm running the PHP CLI through a NSTask in MacOS, but this question is more about the CLI itself.

I'm listening to the stderr pipe, but nothing is output there no matter what file I try to run:

  • If the file type is not a plain text, stdout sets to ?.
  • If the file is a php script with errors, the error messages are still printed to stdout.

Is there a switch to the interpreter to handle errors through stderr? Do I have an option to detect errors other than parsing stdout?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The display_errors directive (can be set everywhere) takes optionally the parameter "stderr" for it to report errors to stderr instead of stdout or completely disabled error output. Quoting from the PHP manual entry:

Value "stderr" sends the errors to stderr instead of stdout. The value is available as of PHP 5.2.4.

Alternatively if you're using the commandline interface and you want to output the errors your own you can re-use the command-line nput/output streams:

fwrite(STDERR, 'error message');

Here STDERR is an already opened stream to stderr.

Alternatively if you want to do it just for this script and not in CLI you can open a filed handler to php://stderr and write the error messages there.

$fe = fopen('php://stderr', 'w');
fwrite($fe, 'error message');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...