‘AND’ vs ‘&&’ operators in PHP

PHPServer Side ProgrammingProgramming

The difference is the precedence when we compare AND with && operator. The precedence of AND operator is lower than the operator = when the evaluation is performed, therefore even if both the operators do the same work, the result is different.

Example

Let us first see an example of the AND operator−

 Live Demo

<?php
   $val1 = 55;
   $val2 = 65;
   if ($val1 == 55 and $val2 == 65)
      echo "Result = True";
   else
      echo "Result = False";
?>

Output

This will produce the following output−

Result = True

Example

Let us now see an example of the && operator−

 Live Demo

<?php
   $val1 = 110;
   $val2 = 110;
   if ($val1 == 110 && $val2 == 110)
      echo "Result = True";
   else
      echo "Result = False";
?>

Output

This will produce the following output:

Result = True

Example

Let us now see the difference in a single example −

 Live Demo

<?php
   $bool = TRUE and FALSE;
   echo ($bool ? 'true' : 'false'), "\n";
   $bool = TRUE && FALSE;
   echo ($bool ? 'true' : 'false');
?>

Output

This will produce the following output−

true
false
raja
Published on 24-Dec-2019 16:35:48
Advertisements