What is a Logical Operator in PHP?

Last updated on September 6, 2023

The Logical Operator is mainly used in PHP to perform logical operations on one or more expressions. PHP provides several logical operators that allow us to compares the values and make decisions based on conditions.

Below is the table containing Logical Operators along with their syntax and operations.

andAnd$x and $yTrue if both the operands are true else false
orOr$x or $yTrue if either of the operands is true else false
xorXor$x xor $yTrue if either of the operands is true and false if both are true
&&And$x && $yTrue if both the operands are true else false
||Or$x || $yTrue if either of the operands is true else false
!Not!$xTrue if $x is false
Logical operators list

Return Values: The comparison operators return bool (true or false).

Let’s try to understand with the following examples,

1. And (&) Operator:

The AND operator returns true if both variable’s comparisons are true.

$x = 5; 
$y = 10; 
if ($x > 0 && $y > 0) { 
    echo "Both conditions are true!"; 
// output: Both conditions are true! 

In this example, the AND (&) operator verifies that both variables $x and $y are greater than 0.

2. Or (or) Operator:

If either of the conditions is true. The OR Operator returns values true.

$x = 100;
$y = 50;
if ($x == 100 or $y == 80) {
   echo "One of condition is true";
//Output: XOR is TRUE.

In the above PHP example, if either $x is equal to 100 or $y is equal to 80, it will output “One of the conditions is true”.

3. Xor (xor) Operator:
When either of the variables are true the XOR condition returns true. It returns false when both conditions are true or false.

$x = 100;
$y = 50;
if ($x == 100 xor $y == 80) {
    echo "One of them is true";
// output: One of them is true

In this example, XOR (xor) operator to check whether exactly one of the conditions is true, and it will output “One of them is true” when either $x is equal to 100 or $y is equal to 80 because only one of these conditions is true. If both conditions were true or both were false, the XOR operator would return false, and the message would not be displayed.


This article demonstrates logical operators along with examples. Also it has table containing types of logical operators along with name, symbol and definition.

Written by
I am a web developer with over 2 years of industry experience, specializing in PHP, JavaScript, MySQLi, and Laravel. I create dynamic and user-friendly web applications, committed to delivering high-quality digital solutions.

Share on:

Related Posts

Improved by: Ayaz Ali Shah

Tags: PHP,