Difference between & and && in PHP
Solution 1:
&
is bitwise AND. See Bitwise Operators. Assuming you do 14 & 7
:
14 = 1110
7 = 0111
---------
14 & 7 = 0110 = 6
&&
is logical AND. See Logical Operators. Consider this truth table:
$a $b $a && $b
false false false
false true false
true false false
true true true
Solution 2:
The other answers are correct, but incomplete. A key feature of logical AND is that it short-circuits, meaning the second operand is only evaluated if necessary. The PHP manual gives the following example to illustrate:
$a = (false && foo());
foo
will never be called, since the result is known after evaluating false. On the other hand with
$a = (false & foo());
foo
will be called (also, the result is 0 rather than false).