【问题标题】:PHP Error Reporting - use of "^" character and the "~" characterPHP 错误报告 - 使用“^”字符和“~”字符
【发布时间】:2012-11-21 16:44:17
【问题描述】:

我试图了解在设置 error_reporting 值时使用“^”字符和“~”字符之间的区别。例如,我的 php 脚本中有以下内容:

if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
error_reporting(E_ALL & ~ E_DEPRECATED & ~ E_USER_DEPRECATED & ~ E_NOTICE);
} else {
error_reporting(E_ALL ^ E_NOTICE);
}

我已阅读手册页:

http://php.net/manual/en/function.error-reporting.php

但我现在比以往任何时候都更加困惑。是:

error_reporting(E_ALL & ~ E_DEPRECATED & ~ E_USER_DEPRECATED & ~ E_NOTICE);

同:

error_reporting(E_ALL ^ E_DEPRECATED ^ E_USER_DEPRECATED ^ E_NOTICE);

【问题讨论】:

    标签: php error-reporting


    【解决方案1】:

    这些是位运算符:http://php.net/manual/en/language.operators.bitwise.php

    error_reporting(E_ALL & ~ E_DEPRECATED & ~ E_USER_DEPRECATED & ~ E_NOTICE);
    

    表示 E_ALL 而不是 E_DEPRECATED 而不是 E_USER_DEPRECATED 而不是 E_NOTICE

    同时

    error_reporting(E_ALL ^ E_DEPRECATED ^ E_USER_DEPRECATED ^ E_NOTICE);
    

    表示 E_ALL 除了 E_DEP.... 等

    【讨论】:

    • ^ 不是or,而是xor
    • 谢谢 - 所以我的结论是正确的 (E_ALL & ~ E_DEPRECATED & ~ E_USER_DEPRECATED & ~ E_NOTICE) 产生与 (E_ALL ^ E_DEPRECATED ^ E_USER_DEPRECATED ^ E_NOTICE) 相同的结果吗?
    【解决方案2】:

    我认为与您的问题更相关的答案已在error reporting page on php.net 的评论中列出,我将在此处重新发布:

    对于我们这些不完全熟悉按位运算符的人来说,E_ALL ^ E_NOTICE 的示例“有点”令人困惑。

    如果您希望从当前级别删除通知,无论未知级别是什么,请使用 & ~ 代替:

    <?php
    //....
    $errorlevel=error_reporting();
    error_reporting($errorlevel & ~E_NOTICE);
    //...code that generates notices
    error_reporting($errorlevel);
    //...
    ?>
    

    ^ 是 xor(位翻转)运算符,如果之前关闭通知(在其左侧的错误级别),则实际上会将通知打开 on。它在示例中有效,因为 E_ALL 保证设置了 E_NOTICE 位,因此当 ^ 翻转该位时,它实际上已关闭。 & ~ (and not) 将始终关闭右手参数指定的位,无论它们是打开还是关闭。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多