Sunday 30 June 2019

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?


Answer



& 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


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...