Last updated on September 6, 2023 by Arshad Ullah

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.

Operator | Name | Syntax | Operation |

and | And | $x and $y | True if both the operands are true else false |

or | Or | $x or $y | True if either of the operands is true else false |

xor | Xor | $x xor $y | True if either of the operands is true and false if both are true |

&& | And | $x && $y | True if both the operands are true else false |

|| | Or | $x || $y | True if either of the operands is true else false |

! | Not | !$x | True if $x is false |

**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.

**Conclusion:**

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

Share on:

Related Posts

Published on 25 September

Published on 21 September

Published on 19 September

Published on 18 September

Published on 17 September