PHP CLI不使用stderr来输出错误 [英] PHP CLI doesn't use stderr to output errors

查看:156
本文介绍了PHP CLI不使用stderr来输出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过MacOS中的NSTask运行PHP CLI,但是这个问题更多地与CLI本身有关.

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

我正在监听stderr管道,但是无论我尝试运行哪个文件,都不会输出任何内容:

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

  • 如果文件类型不是纯文本,则stdout设置为?.
  • 如果文件是带有错误的php脚本,则错误消息仍会打印到stdout.
  • 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.

是否可以切换到解释器以通过stderr处理错误?除了解析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?

推荐答案

display_errors指令(可以在任何地方设置)可选地使用参数"stderr",以将错误报告给 stderr ,而不是 stdout 或完全禁用的错误输出.引用PHP手册条目:

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:

"stderr" 将错误发送到 stderr 而不是 stdout .该值自PHP 5.2.4起可用.

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');

这里STDERR是已经打开的 stderr 流.

或者,如果您只想为该脚本而不是在CLI中执行此操作,则可以将文件处理程序打开到php://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');

这篇关于PHP CLI不使用stderr来输出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆