Wednesday 28 January 2009

&, ampersand, in php

in the following piece of code:

$a=10 ; //(1010b)
$b = 0; // (0000b)

if ($a & $b)

is different from

if ($a && $b)

The first one is true and the second if false.

When
we use only one & it means bit-by-bit and operation, so $a &
$b=10 (1010b), while when you use && it means and operator of
clauses and in this case $a && $b = 0, cause $b=0.

Beware of the signs. This FAQ is also valid for the or operator ('|').

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter