2012-12-04 28 views
3

我试图了解设置error_reporting值时使用“^”字符和“〜”字符的区别。例如,我已在我的PHP脚本如下: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); 

回答

2

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

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

将意味着E_ALL和NOT E_DEPRECATED和NOT E_USER_DEPRECATED &不E_NOTICE

error_reporting(E_ALL^E_DEPRECATED^E_USER_DEPRECATED^E_NOTICE); 

将意味着除了E_ALL .... E_DEP等

+1

'^'不是'或',它是'xor'。 – DCoder

+0

是的,我刚刚意识到这一点,并修复。谢谢 – kennypu

+0

谢谢 - 所以我正确的结论是(E_ALL&〜E_DEPRECATED&〜E_USER_DEPRECATED&〜E_NOTICE)产生与(E_ALL^E_DEPRECATED^E_USER_DEPRECATED^E_NOTICE)相同的结果? – user982124

0

我认为更相关的回答你的问题是在对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); 
//... 
?> 

^是异或(位翻转)操作,实际上会变成通知如果他们以前关闭(在其左侧的错误级别)。它在这个例子中起作用,因为E_ALL保证设置E_NOTICE的位,所以当^翻转时,它实际上被关闭。 &〜(而不是)将总是关闭由右侧参数指定的位,无论它们是否打开或关闭。