You need && for “and” logic and || for “or” logic. That’s the whole answer.
<?php
$a = 10;
if ($a > 0 && $a < 100) {
echo '$a variable is between 0-100';
}
?>
If both conditions are true, the code inside runs. If either one fails, it doesn’t. Simple.
Basic logical operators in PHP
You have three main tools here: &&, ||, and !.
&& means “and.” Both sides must be true.
|| means “or.” At least one side must be true.
! negates a single value. It flips true to false, and false to true.
I see people try to use the words and and or in PHP. You can. But I don’t recommend it. There’s a precedence trap waiting there that bites you when you least expect it. Stick to the symbols. They’re explicit, they’re standard across languages, and they avoid the subtle bugs that come from mixing word operators with assignment.
Precedence: && binds tighter than ||
This trips people up constantly.
&& has higher precedence than ||. That means if you mix them without parentheses, PHP groups the && first.
$a || $b && $c
That is not ( $a || $b ) && $c. It’s $a || ( $b && $c ).
I’ve debugged logic errors that came down to exactly this. The code looked right. It ran wrong. Adding parentheses fixed it instantly.
My rule: if you mix && and ||, add parentheses. Even if you think you know what it means. Future you will thank present you when the logic is obvious at a glance.
if ($isAdmin || ($isEditor && $hasAccess)) {
// ...
}
That reads clearly. No mental gymnastics required.
Short-circuit evaluation: put cheap checks first
PHP stops evaluating a logical expression as soon as the result is determined. That’s called short-circuiting.
It matters when one side of the check could be expensive or risky.
if (isset($user) && $user['active']) {
// safe to use $user
}
If $user isn’t set, PHP never touches $user['active']. No undefined index notice. No crash. You saved yourself a warning just by ordering the checks correctly.
I’ve done this wrong before. I’d write $user['active'] && isset($user) and wonder why I got notices in production. The order is not cosmetic. It’s functional. Put the cheap, safe check first.
Check one value against many: use in_array
I used to write chains of || like this:
if ($role === 'admin' || $role === 'editor' || $role === 'moderator') {
// ...
}
It works. But it’s noisy, and it gets worse every time you add a role.
Use in_array instead:
$allowed = ['admin', 'editor', 'moderator'];
if (in_array($role, $allowed, true)) {
// ...
}
The third argument, true, enforces strict comparison. That means '1' doesn’t match 1. I always pass it. Loose comparison in an array check is a classic source of weird bugs.
Too many nested ifs: use guard clauses
Deep nesting makes code hard to follow. I’ve seen functions with four or five levels of if inside each other. My brain gives up around level three.
Here’s the before:
function processOrder($order) {
if ($order !== null) {
if ($order['status'] === 'pending') {
if (isset($order['items'])) {
// actual logic lives here, buried
chargeCustomer($order);
}
}
}
}
Here’s the after:
function processOrder($order) {
if ($order === null) return;
if ($order['status'] !== 'pending') return;
if (!isset($order['items'])) return;
chargeCustomer($order);
}
Same logic. Way easier to read. Each guard clause handles one failure case and gets out of the way. The happy path sits at the top level, where it belongs.
PHP 8: match(true) for ranges
If you’re on PHP 8, match is a cleaner alternative to long if/else chains, especially for range checks.
$a = 10;
$status = match(true) {
$a < 0 => 'negative',
$a >= 0 && $a <= 100 => 'valid range',
default => 'too large',
};
echo $status; // valid range
It’s not magic. It still evaluates conditions in order and stops at the first match. But it removes the brace nesting and makes the mapping between condition and result obvious.
I use it for status checks, permission tiers, anything where I’m mapping a value to one of several labels. It reads like a table, which is how my brain actually thinks about these cases.
Keep it flat, keep it explicit
The core idea hasn’t changed since that first example. Combine conditions with && and ||. Add parentheses when you mix them. Order your checks so short-circuiting works in your favor. And when the logic gets too nested, flatten it with early returns or match.
None of this is complicated. It’s just discipline. Write the condition, read it back out loud, and make sure a new developer could understand it without you standing over their shoulder.