1

I've come across some unusual PHP code in a system that I have inherited.

To simplify it, the code reads:

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

and continues like this for a few lines.

I cant seem to find anything in the PHP manual that corresponds to this type of operator. I would appreciate even a link to an article or some documentation that explains the outcome of this code.

RonnyKnoxville
  • 6,166
  • 10
  • 46
  • 75
  • If you visit the "operators" page you will find a table of [operator precedence](http://www.php.net/manual/en/language.operators.precedence.php). The rest is easy. – Jon Oct 24 '13 at 14:14

3 Answers3

2

You're looking for PHP's Bitwise Operators.

Anthony Sterling
  • 2,451
  • 16
  • 10
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)
Hisham
  • 455
  • 4
  • 16
1

This is bitwise comparison of the numbers - but it does not seem to make sense to compare static numbers.

Here's a lot of background about it: http://php.net/manual/en/language.operators.bitwise.php