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 || (Logical OR) operator is a binary boolean operator that evaluates to true if at least one of its operands evaluates to true, and false strictly when both operands evaluate to false.
$result = $expression1 || $expression2;

Evaluation Mechanics

Short-Circuit Evaluation The || operator processes operands from left to right using short-circuit evaluation. If the left operand evaluates to true, the operator immediately returns true and the right operand is completely ignored (neither executed nor evaluated). The right operand is evaluated if and only if the left operand evaluates to false.
// $b is never evaluated because the left operand is true
$result = true || $b; 
Type Coercion PHP implicitly casts non-boolean operands to boolean values prior to evaluation.
  • Values such as 0, 0.0, "" (empty string), "0", null, and empty arrays [] are coerced to false (falsy).
  • All other values are coerced to true (truthy).
$result = 0 || "text"; // Evaluates to true (false || true)

Operator Precedence

The || operator occupies a specific tier in PHP’s operator precedence hierarchy, which dictates how expressions are grouped in the absence of parentheses.
  • Lower precedence than &&: In expressions combining Logical AND and Logical OR, && binds tighter and is evaluated before ||.
  • Higher precedence than or: While || and or perform the exact same logical operation, || has a higher precedence than assignment operators (=), whereas or has a lower precedence.
// Precedence comparison
$a = false || true; // $a is assigned true. Evaluated as: $a = (false || true)
$b = false or true; // $b is assigned false. Evaluated as: ($b = false) or true

Return Type

Regardless of the original data types of the operands provided, the || operator strictly returns a boolean value (true or false). Unlike some other programming languages (like JavaScript), PHP’s || operator does not return the underlying truthy or falsy operand itself.
$result = "apple" || "orange"; 
// Returns boolean true, NOT the string "apple"
Master PHP with Deep Grasping Methodology!Learn More