PHP @ operator for include不要禁用error_reporting [英] PHP @ operator for include don't disable error_reporting
问题描述
我正在寻找最佳解决方案,如何禁用已包含文件的已知警告(与我的脚本无关)。
I looking for best solution how to disable known warnings (and irrelevant for my script) which is reported from included file.
包含文件的简短示例: p>
Short example of included file:
$ cat incl_file.php
<?php
error_reporting(E_ALL);
ini_set("display_errors", "on");
$x = $y;
?>
所需代码的示例(不阻止显示包含文件中的错误)
Example of desired code (which don't prevent displaying errors from included file)
$ cat main2.php
<?php
error_reporting(E_ALL);
ini_set("display_errors", "on");
@include_once "incl_file.php";
$d=$e;
print "main_file\n";
?>
输出是:
$ php main2.php
Notice: Undefined variable: y in /tmp/php_hack/incl_file.php on line 6
Notice: Undefined variable: e in /tmp/php_hack/main2.php on line 7
main_file
以下解决方法工作,但我不满意那个混乱
Following "workaround" works but I'm not satisfied with that "mess"
<?php
function myErrorHandler($errno, $errstr, $errfile, $errline) {
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
//print "called $errstr\n";
/* Don't execute PHP internal error handler */
return true;
}
set_error_handler("myErrorHandler");
error_reporting(0);
ini_set("display_errors", "off");
include_once "incl_file.php";
restore_error_handler();
error_reporting(E_ALL);
ini_set("display_errors", "on");
$d=$e;
print "main_file\n";
?>
输出:
$ php main.php
Notice: Undefined variable: e in /tmp/php_hack/main.php on line 24
main_file
如果包含的文件没有
error_reporting(E_ALL);
ini_set("display_errors", "on");
然后@运算符按预期工作...
then @ operator works as expected...
推荐答案
@
操作符仅适用于使用它的表达式。它不能禁用包含文件的错误报告。 PHP不能包含文件时,我会隐藏警告。
@
operator works only for expression with which it was used. It can't disable error reporting for included file. I'll just hide warning when PHP can't include file.
这篇关于PHP @ operator for include不要禁用error_reporting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!