本段摘录自php manual
PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any diagnostic error that might be >generated by that expression will be suppressed. If a custom error handler function is set with set_error_handler(), it will still be called even though the diagnostic has been suppressed, as >such the custom error handler should call error_reporting() and verify that the @ operator was used in the following way:
大意是:在php中使用@符号抑制的错误,仍能触发使用set_error_handler设置的自定义错误处理函数,所以需要在自定义的错误处理函数中过滤掉那些被抑制的错误,代码如下:
<?php
function my_error_handler($err_no, $err_msg, $filename, $linenum) {
if (!(error_reporting() & $err_no)) {
return false; // Silenced
}
// ...
}
?>
被抑制的错误在进入这个函数时,error_reporting()的结果为0,所以整个函数会返回false,于是起到了过滤的作用。
在PHP开发中,尽管使用@符号可以抑制错误显示,但这样的错误仍能被set_error_handler设置的自定义错误处理函数捕获。为了解决这个问题,可以在自定义函数中通过error_reporting()检查,当其结果为0时,表示错误被抑制,从而避免处理这些错误。
1844

被折叠的 条评论
为什么被折叠?



