W3docs

or

The "or" keyword is used in PHP to create logical disjunctions between two or more expressions. In this article, we will explore the syntax and usage of the

The PHP "or" Keyword

The or keyword is a logical operator in PHP: it combines two boolean expressions and returns true when at least one of them is true. It is the word-form sibling of the || operator — the two produce the same logical result, but they differ in operator precedence, which is the single most important thing to understand before you use or.

This page covers the syntax of or, how its precedence trips people up in assignments, its short-circuit behavior, and when you'd reach for or over ||. For the inverse operator, see and; to put these conditions to work, see if and if / else / elseif.

Syntax

if ($expression1 or $expression2) {
  // runs when either expression is true
}

PHP evaluates $expression1 first. If it is already truthy, $expression2 is never evaluated (see Short-circuit evaluation below). The whole expression is true if either operand is truthy, and false only when both are falsy.

$a$b$a or $b
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

or vs. || — the precedence trap

or and || mean the same thing logically, but or has very low precedence — lower than the assignment operator =. That changes how this line is parsed:

<?php
$a = false or true;   // parsed as: ($a = false) or true
var_dump($a);          // bool(false)  ← surprising!

$b = false || true;   // parsed as: $b = (false || true)
var_dump($b);          // bool(true)

With or, the assignment happens before the or is even considered, so $a ends up false. With ||, the comparison happens first. Rule of thumb: use || inside assignments and expressions; reserve or for the control-flow idiom shown below.

Short-circuit evaluation

Like ||, the or operator short-circuits: if the left operand is truthy, PHP does not evaluate the right operand at all. This lets you skip expensive function calls — and is the basis for the or die() guard pattern.

<?php
function check() {
    echo "check() ran\n";
    return true;
}

if (true or check()) {
    echo "matched\n";
}
// Output:
// matched
// (check() never ran, so "check() ran" is NOT printed)

When to use or

Because of its low precedence, or shines in a "do this, or else handle the failure" guard, where you want the assignment/statement on the left to bind first:

<?php
// Classic fallback idiom
$value = null;
$value or print("value was falsy\n");   // prints: value was falsy

// Common in legacy code (modern code prefers exceptions)
// $handle = fopen("data.txt", "r") or die("Cannot open file");

Examples

Let's look at some practical examples of how the "or" keyword can be used:

Examples of PHP or

php— editable, runs on the server

In these examples, or evaluates conditions left-to-right and stops as soon as one is true. If none match, the code block is skipped.

Benefits and trade-offs

  • Readability: Spelling out conditions in plain English (if ($a or $b)) can read more naturally than || to newcomers.
  • Guard-clause idiom: Its low precedence makes the expr or die(...) / expr or handle() pattern concise.
  • Watch out: That same low precedence makes or dangerous in assignments. When in doubt, use ||.

For a full picture of how or sits among PHP's logical and other operators, see PHP Operators. The complementary operator that requires both conditions is and.

Conclusion

The or keyword combines conditions and returns true when either is truthy. It behaves like || logically but has lower precedence than =, so it binds after assignment — use || inside expressions and reserve or for guard-style statements. Combined with short-circuit evaluation, understanding this precedence rule lets you write safer, clearer PHP.

Practice

Practice
In PHP, what does the OR operator do?
In PHP, what does the OR operator do?
Was this page helpful?