PHP Logical Operators
In this page:
if (condition1 && condition2) { } // both true
if (condition1 || condition2) { } // at least one true
if (!condition) { } // not
Logical AND (&&, and)
&& (or the word and) only evaluates to true when both sides are true, and PHP short-circuits it: if the left side is already false, the right side never even runs.
That short-circuiting is why && is commonly used to guard against errors, e.g. isset($x) && $x > 0.
Example: Logical AND (&&, and)
<?php
// Declare `$x`, set to `5`
$x = 5;
// Check whether `isset($x) && $x > 0`
if (isset($x) && $x > 0) {
// Print "x is set and positive" to the output
echo "x is set and positive";
}
?>
Login to try C/C++/Java/PHP code in the editor
Logical OR (||, or)
|| (or or) evaluates to true the moment *either* side is true, and it short-circuits the opposite way — if the left side is already true, PHP never bothers evaluating the right side at all, which can matter if that right side has side effects.
Example: Logical OR (||, or)
<?php
// Declare `$loggedIn`, set to `false`
$loggedIn = false;
// Declare `$isAdmin`, set to `true`
$isAdmin = true;
// Check whether `$loggedIn || $isAdmin`
if ($loggedIn || $isAdmin) {
// Print "Access granted" to the output
echo "Access granted";
}
?>
Login to try C/C++/Java/PHP code in the editor
Logical NOT (!)
! inverts a single boolean on its own — true becomes false and vice versa. It's most often seen prefixing a function call or condition to flip its meaning directly, as in if (!isset($value)), read as 'if value is not set.'
Example: Logical NOT (!)
<?php
// Declare `$value`, set to `null`
$value = null;
// Check whether `!isset($value)`
if (!isset($value)) {
// Print "value is not set" to the output
echo "value is not set";
}
?>
Login to try C/C++/Java/PHP code in the editor
Logical XOR (xor)
xor returns true only when exactly one side is true — both true or both false both produce false. It's less common than &&/|| in everyday code, but it's exactly the right tool when a rule genuinely means 'one or the other, but not both.'
Example: Logical XOR (xor)
<?php
// Print a detailed dump (with types) of `true xor false`
var_dump(true xor false);
// Print a detailed dump (with types) of `true xor true`
var_dump(true xor true);
?>
Login to try C/C++/Java/PHP code in the editor
Combining Logical Operators
PHP evaluates logical operators left to right according to precedence rules, and those rules aren't always what you'd guess — and/or bind more loosely than &&/||.
Wrapping sub-expressions in parentheses removes any ambiguity and makes the intended grouping obvious to a reader too.
Example: Combining Logical Operators
<?php
// Declare `$a`, set to `true`
$a = true;
// Declare `$b`, set to `false`
$b = false;
// Declare `$c`, set to `true`
$c = true;
// Print `($a && $b) || $c ? "true" : "false"` to the output
echo ($a && $b) || $c ? "true" : "false";
?>
Login to try C/C++/Java/PHP code in the editor
- Mixing
and/orwith=, as in$ok = true and false;, where$okbecomestruebecauseandhas lower precedence than assignment. - Expecting both sides of
&&to always run, when short-circuiting skips the right side if the left is false, so a function call there may not execute. - Confusing
||with??, so$x || defaultreturns a booleantrueinstead of the fallback value.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: