and
Learn the PHP and logical operator — how it differs from &&, operator precedence, short-circuit evaluation, the assignment gotcha, and practical examples.
The PHP and Operator
PHP has two logical AND operators: && and and. Both return true only when both operands evaluate to true, and false otherwise. The difference is operator precedence: && binds much more tightly than and. That single difference is responsible for almost every bug involving and, so this page covers it carefully.
This guide explains the syntax of and, how short-circuit evaluation works, why precedence matters, and the classic assignment trap to avoid.
Syntax
The and operator combines two conditions into one boolean expression. It is most often used inside an if statement to require that several conditions hold at once:
<?php
if (condition1 and condition2) {
// Runs only if BOTH conditions are true
}The body runs only when both operands are truthy. The expression condition1 and condition2 itself evaluates to a boolean (true or false), so you can also store the result in a variable — but watch out for the assignment gotcha described below.
How and behaves
$a | $b | $a and $b |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
PHP evaluates and from left to right and short-circuits: if the left operand is falsy, the right operand is never evaluated, because the result can no longer be true. This is useful for guarding an expensive or unsafe check behind a cheap one:
<?php
$user = null;
// isLoggedIn() is never called because $user is null,
// so we avoid a "method on null" error.
if ($user !== null and $user->isLoggedIn()) {
echo "Welcome back." . PHP_EOL;
} else {
echo "Please log in." . PHP_EOL;
}
// Output: Please log in.Examples
<?php
// Example 1
$age = 25;
$hasID = true;
if ($age >= 18 and $hasID) {
echo "You may enter the club." . PHP_EOL;
}
// Output: You may enter the club.
// Example 2
$username = "admin";
$password = "password123";
if ($username == "admin" and $password == "password123") {
echo "Access granted." . PHP_EOL;
}
// Output: Access granted.
// Example 3
$isWeekday = true;
$time = "12:30";
if ($isWeekday and $time >= "09:00" and $time <= "17:00") {
echo "The office is open." . PHP_EOL;
}
// Output: The office is open.Each if combines several conditions with and, and the body runs only when all of them are true.
Precedence: and vs &&
and has very low precedence — lower than ||, lower than =, and lower than or (and is higher than or, but both sit near the bottom of the precedence table). The && operator, by contrast, sits high — just below the comparison operators. Mixing logical operators with arithmetic, assignment, or or therefore often needs parentheses.
When and and or appear together, and is evaluated first:
<?php
$age = 16;
$hasID = false;
$isStudent = true;
// Reads as: ($age >= 18 and $hasID) or $isStudent
if ($age >= 18 and $hasID or $isStudent) {
echo "Access granted." . PHP_EOL;
}
// Output: Access granted.Add parentheses to make the grouping explicit or to override it:
<?php
$age = 16;
$hasID = false;
$isStudent = true;
// Force the OR to be checked first
if ($age >= 18 and ($hasID or $isStudent)) {
echo "Access granted." . PHP_EOL;
} else {
echo "Access denied." . PHP_EOL;
}
// Output: Access denied.The assignment gotcha
This is the trap that makes most developers prefer &&. Because and has lower precedence than =, the assignment happens before the and:
<?php
$a = true;
$b = false;
// Parsed as: ($result = $a) and $b
$result = $a and $b;
var_dump($result); // bool(true) — NOT false!
// With && (higher precedence than =) you get what you expect:
$result = $a && $b;
var_dump($result); // bool(false)In the first case, $result is assigned $a (true) first, and the and $b part is evaluated and discarded. Use && for boolean expressions, and reserve and for cases where the low precedence is intentional.
When to use and
- Prefer
&&for the value of a boolean expression (anything you assign or return). andis handy as a control-flow connector where its low precedence is harmless and reads well, for example:$db->connect() or die('No DB')usesorthe same way.- Within an
if (...)condition with no=orormixed in,andand&&behave identically.
Related operators
or— logical OR (true if either operand is true).xor— logical exclusive OR (true if exactly one operand is true).- PHP operators — the full operator reference.
- PHP if...else...elseif — building conditional logic.
Conclusion
The and operator combines conditions and returns true only when both are true, short-circuiting on a falsy left operand. It is functionally the same as && but has much lower precedence, which is exactly why $x = $a and $b does not do what it looks like. Use && for boolean values and choose and deliberately when its low precedence is what you want.