Home / Programming / PHP / PHP Logical Operators

PHP Logical Operators

Published On: November 2nd, 2019|Categories: PHP|Tags: |1 min read|

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.

OperatorNameExampleResult
andAnd$x and $yTrue if both $x and $y are true
orOr$x or $yTrue if either $x or $y is true
xorXor$x xor $yTrue if either $x or $y is true, but not both
&&And$x && $yTrue if both $x and $y are true
||Or$x || $yTrue if either $x or $y is true
!Not!$xTrue 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: