2013-10-24 55 views
1

我遇到了一些我在继承的系统中不常见的PHP代码。PHP&and |运算符

为了简化计算,代码如下:

$test_array['first_element'] = 1 | 2; 
$test_array['second_element'] = 3 & 1; 

,并继续像这样几行。

我似乎找不到对应于这种类型的操作符的PHP手册中的任何东西。我很感激即使是链接到文章或解释此代码结果的一些文档。

+0

如果您访问“操作员”页面,你会发现[运算符优先级]表(http://www.php.net/manual/ EN/language.operators.precedence.php)。其余的很容易。 – Jon

回答

2
& (Bitwise AND) 

Performs the AND operation on each pair of bits. a AND b yields 1 only if both a and b are 1 
e.g: 
    0101 (decimal 5) 
AND 0011 (decimal 3) 
    = 0001 (decimal 1) 

| (Bitwise OR) 

Performs the OR operation on each pair of bits. a OR b yields 1 if either a or b is 1 
e.g: 
    0101 (decimal 5) 
OR 0011 (decimal 3) 
= 0111 (decimal 7)