Difference between & and && in PHP
I am confused with &
and &&
. I have two PHP books. One says that they are same, but the another says they are different. I thought they are same as well.
Aren't they same?
&
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
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).
AND operation: & -> will do the bitwise AND operation , it just doing operation based on the bit values. && -> It will do logical AND operation. It is just the check the values is true or false. Based on the boolean value , it will evaluation the expression链接地址: http://www.djcxy.com/p/1692.html
上一篇: PHP中的“&”是什么意思?
下一篇: PHP中&和&&的区别