PHP Logical Operators - Logical Operators in PHP



PHP Logical Operators

  • PHP Logical Operators are operators that allow you to compare two values and return a Boolean (true or false) based on the comparison. Examples of PHP Logical Operators include && (AND), || (OR), ! (NOT), and XOR (exclusive OR).
Operator Name Syntax Operation
and Logical AND $a and $b True if both the operands are true else false
or Logical OR $a or $b True if either of the operands is true else false
xor Logical XOR $a xor $b True if either of the operands is true and false if both are true
&& Logical AND $a && $b True if both the operands are true else false
|| Logical OR $a || $b True if either of the operands is true else false
! Logical NOT !$a True if $a is false
php-logical-operators.gif

Sample Code

<?php
$a = 50;
$b = 30;
if ($a == 50 and $b == 30)
	echo "and Success <br>";
if ($a == 50 or $b == 20)
	echo "or Success <br>";
if ($a == 50 xor $b == 20)
	echo "xor Success <br>";
if ($a == 50 && $b == 30)
	echo "&& Success <br>";
if ($a == 50 || $b == 20)
	echo "|| Success <br>";
if (!$c)
	echo "! Success <br>";
?>

Output

and Success
or Success
xor Success
&& Success
|| Success
! Success

Related Searches to PHP Logical Operators - Logical Operators in PHP