Logical Operators in PHP

Logical operators in PHP are those operators which are used to form compound conditions by combining two or more conditional statements.

In other words, logical operators allow you to compare conditions or values. These operators are also called Boolean operators because they return a Boolean value (true or false) based on the evaluation of the conditions.

Logical operators first convert their operands to Boolean form and then perform the respective comparison. They are useful in PHP when you need to check more than one condition simultaneously and want to use the outcome.

A simple example of logical operators in PHP is given below:

$x > $y && $x > $z

This expression combines two relational expressions using a logical operator (&&). Such an expression is called a logical expression. The result of a logical expression is also either true or false, depending on the evaluation of the conditions.

Types of Logical Operators in PHP


PHP language provides six main logical operators:

  • AND
  • OR
  • XOR
  • NOT (!)
  • &&
  • ||

Logical AND Operator

The logical AND operator evaluates two operands (conditions) and returns true if the conditions on both sides of && operators are true. If one or both conditions on either side of the operator are false, then it returns false.

Example 1:

<?php
$x = 20;
$y = 10;
var_dump($x == 20 AND $y == 10);
?>
Output:
      bool(true)


Example 2:

<?php
$a = 10;
$b = 20;

if ($a > 5 && $b > 15) {
    echo "Both conditions are true!";
} else {
    echo "One or both conditions are false!";
}
?>
Output:
      Both conditions are true!

You can also create a complex conditions by combining multiple operands or conditions using logical AND operator. You should always use parentheses for clarity, especially when combining multiple conditions. Here’s an example of it.

Example 3:

<?php
$age = 25;
$isEmployed = true;
$hasDrivingLicense = true;

if ($age > 18 && $isEmployed && $hasDrivingLicense) {
     echo "You are eligible for car loan!";
} else {
     echo "You are not eligible for car loan.";
}
?>
Output:
       You are eligible for car loan!

In this example, we have combined three conditions by using logical && operator into one condition group. All three conditions must be true for the person to be eligible for car loan.

Logical OR Operator

The logical OR operator returns true if at least one of the operands (conditions) is true.

Example 4:

<?php
$x = 20;
$y = 40;
var_dump($x == 20 OR $y == 30);
?>
Output:
       bool(true)

In this example, output is true because one of the condition is true.

Example 5:

<?php
$a = 10;
$b = 5;

if ($a > 15 || $b > 3) {
     echo "At least one condition is true!";
} else {
     echo "Both conditions are false!";
}
?>
Output:
      At least one condition is true!

In this example, first condition is true. Therefore, the statement “At least one condition is true!” is displayed on the console.

Example 6:

<?php
$isWeekend = true;
$hasHoliday = false;

if ($isWeekend || $hasHoliday) {
    echo "You can take a break!";
} else {
    echo "It's a workday.";
}
?>
Output:
      You can take a break!

Important Note: PHP Logical AND and OR operators use short-circuit evaluation, meaning that they can give a result even without checking the entire expression. For example, logical OR operator will give you true if the first operand is true, since its condition has already been met.


On the other hand, the Logical AND operator will give you false if the first operand is false. It won’t test the second operand anymore since the assigned condition cannot be met no matter what. Hence, this feature can optimize performance of the code when conditions are complex.

Logical NOT (!) Operator

The logical NOT operator inverts the logical value of the operand. It returns true if the condition is false. If the condition is true, it returns false.

Example 7:

<?php
$x = 40;
$y = 50;
var_dump(!($x == 40 AND $y == 50));
?>
Output:
       bool(false)

In this example, both conditions are true. Therefore, the result is false because the not operator negates it.

Example 8:

<?php
$isLoggedIn = true;
$isVerified = false;

if ($isLoggedIn && !$isVerified) {
    echo "Please verify your account.";
} else {
    echo "Access granted!";
}
?>
Output:
       Please verify your account.

Logical XOR Operator

The XOR logical operator returns true if one and only one of the conditions is true. However, you will get false if both of the conditions are true or false.

Example 9:

<?php
$x = 20;
$y = 10;
var_dump($x == 20 XOR $y == 30);
?>
Output:
      bool(true)

In this example, the output is true because only one of the conditions is true.

Example 10:

<?php
$light1 = true;
$light2 = false;

if ($light1 XOR $light2) {
    echo "Only one light is on.";
} else {
    echo "Either both lights are on or off.";
}
?>
Output:
       Only one light is on.

These are the most common logical operators in PHP, which help you to create more complex and robust conditions in your PHP program.

Logical && Operator

This logical operator performs the same basic function as the AND operator. It will return true only if both conditions on its sides are true. Only the difference between them is that the && operator has a higher precedence than AND operator.

This means that when you will use && operator, it will be evaluated before other operators, like assignment (=). Let’s understand it with the help of an example.

Example 11:

<?php
$result = true && false;
var_dump($result);
?>
Output:
       bool(false)

In this example, the first condition is true, and the second condition is false. Both conditions true && false are evaluated first, resulting in false. After that, the assignment (=) operation happens, and the variable $result is assigned the value false.

Example 12:

<?php
$result = true AND false;
var_dump($result);
?>
Output:
       bool(true)

In this example, the output is true because the AND operator has lower precedence than the assignment operator (=). This means that when you will use the AND operator, it is evaluated after the assignment operation.

Hence, PHP first assigns the value true to the variable $result, and then it evaluates the condition true AND false, which returns false, but this does not affect the assignment. Therefore, $result is still true.

Note:

(1) Use && operator when you need higher precedence than the assignment operator (=). In this case, the logical condition is evaluated first, and then the result of that condition is assigned to the variable.

(2) Use AND operator when you need lower precedence than the assignment operator. In this case, the assignment operation happens first, then the logical operation is evaluated.

Logical || Operator

This logical operator is used for OR operation. It performs the same basic function as an OR operator. The main difference between them lies in their operator precedence.

The || logical operator has a higher precedence than the assignment operator (=). It means that when you use || operator, the logical operation will be evaluated before the assignment operation.

Example 13:

<?php
$result = true || false;
var_dump($result);
?>
Output:
     bool(true)

In this example, the first condition is true, and the second condition is false. Both conditions true || false are evaluated first, which results true. After the evaluation, the assignment will take place. The result is assigned to a variable named $result.

On the other hand, the or logical operator has a lower precedence than the assignment operator (=). This means that the assignment takes place first, and then the logical operation is evaluated.

Example 14:

<?php
$result = false OR true;
var_dump($result);
?>
Output:
       bool(false)

In this example, the assignment operation takes place first, so the value false is assigned to the variable named $result before the logical expression (false or true) is evaluated. After the assignment, the logical operation has performed, which results true value. However, the result of logical operation doesn’t change the value of $result, so it remains false.

Note:

(1) Use || operator when you need higher precedence than the assignment operator (=). In this case, the logical condition is evaluated first, and then the result of that condition is assigned to the variable.

(2) Use OR operator when you need lower precedence than the assignment operator. In this case, the assignment operation takes place first, then the logical operation is evaluated.

Combining Logical Operators in PHP


You can combine logical operators to form complex and robust conditions in your PHP code. Let’s take an example on it.

Example 15:

<?php
$age = 30;
$isStudent = false;
$isEmployed = true;

if (($age > 18 && $age < 60) && ($isStudent || $isEmployed)) {
    echo "You are eligible for benefits!";
}
?>
Output:
      You are eligible for benefits!

In this example, the output is “You are eligible for benefits!” because both conditions ($age > 18 && $age < 60) and ($isStudent || $isEmployed) are true.

Remember that when you combine multiple logical operators, always use parentheses to clarify operator precedence. The logical AND operator has the higher precedence than OR operator.

List of PHP Logical Operators


Here, we have listed all the PHP logical operators with example and description in the table form.

OperatorOperationExampleDescription
andAND$x and $yReturns True if both $x and $y are true, else returns False.
orOR$x or $yReturns True if either of the operands (conditions) is true, else returns False.
xorXOR$x xor $yReturns True if either $x or $y is true. Else returns False if both are true.
!NOT!$xReturns True if $x is not true.
&&AND$x && $yGives True if both operands are true, otherwise, return False.
||OR$x || $yGives True if either $x or $y is true.

In PHP, a value of 0 represents a logical false condition, and a value of 1 represents a logical true condition. Additionally, other values like false, null, empty strings (“”), and empty arrays ([]) are also considered false in PHP, while any non-zero numbers, non-empty strings, and arrays with elements are considered true.