Current Location: Home> Latest Articles> How to Use AND (&&) to Combine Boolean Expressions in PHP

How to Use AND (&&) to Combine Boolean Expressions in PHP

M66 2025-10-27

Overview of the AND Operator (&&) in PHP

In PHP, the AND operator (&&) is used to combine two Boolean expressions and return a Boolean value. The result is TRUE only if both expressions are TRUE; otherwise, it is FALSE. This operator has a higher precedence than OR and supports short-circuit evaluation, meaning if the first expression is FALSE, the second expression will not be evaluated.

Syntax

<span class="fun">$result = $expression1 && $expression2;</span>

Where:

  • $expression1 and $expression2 are the Boolean expressions to combine.
  • $result is a Boolean value that will be TRUE only if both expressions are TRUE; otherwise, it will be FALSE.

Example

$age = 18;
$gender = 'male';

if ($age >= 18 && $gender == 'male') {
    echo "Condition met";
} else {
    echo "Condition not met";
}

In this example, the condition is met only if $age is greater than or equal to 18 and $gender equals 'male'; otherwise, the condition fails.

Important Considerations

  • The AND operator has higher precedence than the OR operator.
  • The AND operator supports short-circuit evaluation. If the first expression is FALSE, the second expression will not be evaluated.
  • You can combine multiple Boolean expressions using the AND operator, for example:
if ($expression1 && $expression2 && $expression3) {
    // ...
}

By using the AND (&&) operator correctly, you can make conditional statements clearer and more efficient for logical control.