@错误抑制运算符和set_error_handler
我遵循良好的编程实践,并将 PHP 错误记录到文件中,而不是将其显示给用户。我为此使用 set_error_handler()
。
现在问题来了。例如,我在某处:
@file_exists('/some/file/that/is/outside/openbasedir.txt');
但是尽管有错误抑制运算符,错误消息仍会记录。我不想那样。我希望抑制的错误不要传递给我的错误处理程序。
I am following good programming practices and I am logging the PHP errors to file instead of displaying it to user. I use set_error_handler()
for that.
Now the problem. For example, I have somewhere:
@file_exists('/some/file/that/is/outside/openbasedir.txt');
But despite the error suppression operator, the error message logs. I don't want that. I want suppressed errors not to pass to my error handler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这个答案适用于 PHP 7:
@
运算符临时将 error_reporting 设置为 0,因此您可以在错误处理程序中测试 error_reporting 的值:或者更好的是,仅记录 error_reporting 中的错误类型:
另外看一下
log_errors
和error_log
选项,用于自动将错误记录到文件或系统日志中。This answer applies at PHP 7:
The
@
operator temporarily sets error_reporting to 0, so you can test the value of error_reporting in your error handler:Or even better, log only error types that are in error_reporting:
Also take a look at the
log_errors
anderror_log
options, for automatically logging errors to a file or to syslog.也适用于 PHP 7 的解决方案
根据 PHP 文档:
来源: http://php.net/manual/en/language.operators.errorcontrol .php
因此,您可以在错误处理程序中使用以下代码:
Solution that also works for PHP 7
According to the PHP docs:
Source: http://php.net/manual/en/language.operators.errorcontrol.php
So you can use the following code in your error handler:
PHP8 行为已更改,错误处理程序中的检查
error_reporting() == 0
不再起作用。 PHP8中检查@抑制错误的方法如下:PHP8 behavior has changed and checking
error_reporting() == 0
in the error handler no longer works. The way to check for errors suppressed with @ in PHP8 is as follows:来自手册:
这意味着其他答案的解决方案将不起作用:(
@-operator变得完全无法使用恕我直言。
要禁用错误,您必须执行以下操作:
From manual:
This means that solutions from other answers will not work :(
The @-operator became completely unusable imho.
To disable errors you will have to do the following:
PHP8 代码 4437 为 @
PHP8 code 4437 for @
您实际上应该避免使用
@
运算符。首先,它很慢,而且我什至称其为有害的。您应该在 php.ini 文件中有两行:
... 或者,如果您无权访问 php.ini 文件,则在 index.php 的顶部(或任何其他引导文件)您应该添加:
You should actually avoid usage of
@
operator. First of all, it is slow, and I would as far as to call it harmful.What you should have instead is in
php.ini
file have two line:... or , if you do not have access to the php.ini file , then at the top of index.php (or any other bootstrap file) you should add :
从 PHP 8 开始 - 他们更改了 error_reporting() 以返回非零值“E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE”,在处理错误控制运算符时为 4437。这有点不正统,但请记住 E_WARNING 和其他值没有设置。我认为这是一个“神奇”常数。
有关如何处理错误控制运算符的说明,请参阅错误控制运算符 ,以及设置错误处理程序第一个示例如何忽略未启用的错误。以下是处理错误控制运算符和未启用的错误的示例代码。首先检查错误控制运算符,然后过滤掉未启用的错误,这一点很重要。
Starting with PHP 8 - They changed error_reporting() to return the non-zero value "E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE" which is 4437 when handling error control operators. This is a bit unorthodox, but keep in mind that E_WARNING and other values are not set. I consider that a "magic" constant.
See Error Control Operators for description how error control operators are handled, and Set Error Handler first example how to ignore errors that are not enabled. Here is sample code to handle both error control operators and errors that are not enabled. It is important to check for error control operator first, then filter out the errors that are not enabled.