-1

Possible Duplicate:
Reference - What does this symbol mean in PHP?

What does adding a '&' mean in this snippet I found?

$i = 10;
if($i&1){
    echo "$i is odd";
}
else {
    echo "$i is even";
}
Community
  • 1
  • 1
enchance
  • 29,075
  • 35
  • 87
  • 127

2 Answers2

3

It's the bitwise AND operator. in your case, it takes the binary representations of 10 and 1 and performs the logical AND operation on the individual bits.

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
0

That's a binray and. So for example 148 (binary 10010100) & 136 (binary 10001000) will be 128 (binary 10000000). So $i & 1 is either 1 (true) or 0 (false)

Pieter Bos
  • 1,554
  • 1
  • 12
  • 20