← Back to PHP Course | Chapter 3: Operators | Lesson 3 of 8

PHP Logical Operators

Logical operators join yes-or-no questions together, like 'do I have my coat AND is it raining?'. They help PHP make decisions from several conditions at once.
Syntax
php
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
<?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";
}
?>

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
<?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";
}
?>

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
<?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";
}
?>

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
<?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);
?>

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
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Mixing and/or with =, as in $ok = true and false;, where $ok becomes true because and has lower precedence than assignment.
  2. 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.
  3. Confusing || with ??, so $x || default returns a boolean true instead of the fallback value.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.