Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The or operator in PHP is a logical disjunction operator that evaluates two operands and returns true if at least one of the operands evaluates to true, and false otherwise. It employs short-circuit evaluation, meaning the PHP engine will not evaluate the right operand if the left operand already resolves to true.

Syntax

$expr1 or $expr2

Truth Table

The operator follows standard boolean logic for disjunction:
$expr1$expr2Result
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse
Note: PHP will implicitly cast non-boolean operands to booleans (type juggling) before evaluation.

Operator Precedence (or vs ||)

PHP provides two logical OR operators: or and ||. While they perform the exact same logical operation, they possess significantly different operator precedence. The or operator has lower precedence than the assignment operator (=), whereas the || operator has higher precedence than the assignment operator. This structural difference dictates how expressions are bound during parsing.
// Using || (Higher precedence than =)
$resultA = false || true; 
// Evaluated as: $resultA = (false || true)
// $resultA is assigned boolean true

// Using 'or' (Lower precedence than =)
$resultB = false or true; 
// Evaluated as: ($resultB = false) or true
// $resultB is assigned boolean false
To force the or operator to evaluate before assignment, explicit grouping via parentheses is required:
$resultC = (false or true);
// $resultC is assigned boolean true

Short-Circuit Evaluation Mechanics

Because or requires only one true operand to return true, the PHP parser optimizes execution by reading left to right. If the left operand evaluates to true, the overall expression is guaranteed to be true. Consequently, the right operand is completely ignored.
// The function terminate_process() is never executed 
// because the left operand evaluates to true.
true or terminate_process();
Master PHP with Deep Grasping Methodology!Learn More