PHP Logical Operators

Logical operators operate on conditional statements(these are based on conditions) or expressions. Logical operators supported by PHP are And, Or, Xor,&&,||,!.

OperatorName Example Description
andLogical AND$m and $n Returns true if both of the variables $m and $n are true else it returns false.
orLogical OR$m or $n Returns true if either of the variables $m and $n are true else it returns false.
xorLogical XOR$m xor $n Returns true if either of the variables $m and $n are true and else it returns false.
&&Logical AND$m && $nReturns true if both the variables $m and $n are true else it returns false.
||Logical OR$m || $n Returns true if either of the variables $m and $n are true else it returns false.
!Logical NOT!$mReturns true if $m is false

PHP - And Operator

True if both a and b are true.

			

<?php
$a = 100;  
$b = 80;

if ($a == 100 and $b == 80) {
    echo "PHP";
}
?>

				

Output:

PHP

PHP - Or Operator

True if either a or b is true.

			

<?php
$a = 100;  
$b = 80;

if ($a == 100 or $b == 50) {
    echo "PHP";
}
?> 

				

Output:

PHP

PHP - Xor Operator

True if either a or b is true but not both true.

			

<?php
$a = 100;  
$b = 80;

if ($a == 100 xor $b == 50) {
    echo "PHP";
}
?> 

				

Output:

PHP

PHP - && Operator

True if both a and b are true and non zero.

			

<?php
$a = 100;  
$b = 90;
if ($a == 100 && $b == 90) {

    echo "PHP";
}
?>

				

Output:

PHP

PHP - || Operator

True if either a or b is true and non zero.

			

<?php
$a = 100;  
$b = 80;

if ($a == 100 || $b == 50) {
    echo "PHP";
}
?>  

				

Output:

PHP

PHP - ! Operator

True if a is not true.

			

<?php
$a = 100;  

if ($a !== 90) {
    echo "PHP";
}
?> 

				

Output:

PHP