operator precedence how does AND differ from &&

A very common question for a developer is how does AND differ from && ?

Answer for this is, they do the same thing, but differ in their precedence.

&& has higher precedence than AND

For example when comparing AND operator and && operator with = operator we will know exact difference between them:

$a = true;
$b = false;

$c = $a && $b; // && operator is used
$d = $a and $b; // and operator is used

echo “c =>”.$c; // $c is false

echo “d =>”.$d; // $d is true

Since && has higher precedence than AND operator and = operator, the statement

$c = $a && $b; will become $c = ($a && $b); which makes $c as false

Since AND operator has lower precedence than && and = operators

$d = $a and $b; will become ($d = $a) and $b; which makes $d as true.

To know more about operator precedence go to the this link

Leave a Reply

Your email address will not be published. Required fields are marked *