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.

Advertisements
Jetpack

Recent Articles

Categories

Jetpack

Related Articles

If you enjoyed reading this, then please explore our other articles below:

Comments

Leave a comment

Leave a Reply

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

Advertisements