You are currently viewing Logical Operators in PHP | Explained
  • Post category:PHP

Logical Operators in PHP | Explained

Logical Operators in PHP

In this article, we will see logical operators in php with example codes. Logical operators are also known as logical connectives. They are used to connect one or more conditions. They accept Boolean operands, and on evaluation, they yield the result as true or false.

Types of logical operators

&& / AND

If both LHS and RHS operands are true then the result will be true, in all other cases the result will be false. In place of pair of ampersand symbols, we can also use the “and” keyword.

Example:

<?php
echo ((3<4) && (4<5)) ? "true" : "false"; //true
echo ((3<4) and (4<5)) ? "true" : "false"; //true
?>

|| / OR

If both LHS and RHS operands are false then the result will be false, in all other cases the result will be true. In place of pair of pipeline symbols, we can also use the “or” keyword.

Example:

<?php
echo ((3<4) || (4>5)) ? "true" : "false"; //true
echo ((5>10) or (4<5)) ? "true" : "false"; //true
?>

XOR: Logical exclusive OR operator

If both are the same then the result will be false otherwise true.

Example:

<?php
echo ((3<4) xor (4<5)) ? "true" : "false"; //false
echo ((5>10) xor (4<5)) ? "true" : "false"; //true
?>

Leave a Reply