包含的PHP @运算符不会禁用error_reporting [英] PHP @ operator for include doesn't disable error_reporting

查看:44
本文介绍了包含的PHP @运算符不会禁用error_reporting的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找最佳解决方案,如何禁用从包含文件中报告的已知警告(以及脚本的不相关警告).

I am looking for best solution how to disable known warnings (and irrelevant warnings for my script) reported from an included file.

包含文件的简短示例:

$ cat incl_file.php 
<?php
error_reporting(E_ALL);
ini_set("display_errors", "on");


$x = $y;

?>

所需代码示例(这不会阻止显示包含文件中的错误)

Example of the desired code (which doesn't prevent displaying errors from the 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

以下解决方法"有效,但我对此感到不满意:

The Following "workaround" works but I'm not satisfied with it's 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 the @ 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 @运算符不会禁用error_reporting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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