在PHP条件语句中,一个&符号是什么意思?


What does a single ampersand mean in a PHP conditional statement?

以下代码在条件检查中使用单个&。这里的"&"符号是什么意思?

if( $some_array_or_other_var & SOME_CONSTANT_VARIABLE ){ 
    //do something here 
}

它看起来不像一个引用,这就是我困惑的地方。

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

如果按位与运算后的结果为"真",则满足该子句。

例如:

3 & 2 == 2 // because, in base 2, 3 is 011 and 2 is 010
4 & 1 == 0 // because, in base 2, 4 is 100 and 1 is 001

这通常用于检查bitset中的单个位,通过测试2的幂,您实际上是在检查是否设置了特定的位。