PHP Logical Operators
What are PHP logical operator?
PHP Comparison Operators are used to compare two values (integer, a.k.a. number or string a.k.a. text). The operators are AND, OR, XOR, NOT.
Operator | Name | Example | Result |
---|---|---|---|
and | And | $x and $y | True if both $x and $y are true |
or | Or | $x or $y | True if either $x or $y is true |
xor | Xor | $x xor $y | True if either $x or $y is true, but not both |
&& | And | $x && $y | True if both $x and $y are true |
|| | Or | $x || $y | True if either $x or $y is true |
! | Not | !$x | True if $x is not true |
Examples:
<?php # Example 1 $x = 10; $y = 5; if ($x == 10 and $y == 5) { echo "Condition met! Hello world!"; } if ($x == 10 && $y == 5) { echo "Condition met! Hello world!"; } # Example 2 $x = 10; $y = 5; if ($x == 10 or $y == 5) { echo "Condition met! Hello world!"; } if ($x == 10 || $y == 5) { echo "Condition met! Hello world!"; } # Example 3 $x = 100; $y = 50; if ($x == 100 xor $y == 60) { echo "Condition met! Hello world!"; } # Example 4 $x = 10; if ($x !== 90) { echo "Condition met! Hello world!"; } ?>
Learn more about Logical Operators in the official PHP documentation here.
Related Articles
If you enjoyed reading this, then please explore our other articles below:
More Articles
If you enjoyed reading this, then please explore our other articles below: