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 > (greater than) operator is a binary relational operator that evaluates whether the value of its left operand is strictly larger than the value of its right operand. It returns a boolean true if the condition is met, and false otherwise.
$result = $expression1 > $expression2;
Because PHP is a dynamically typed language, the > operator utilizes internal type juggling (coercion) when evaluating operands of differing data types. The Zend Engine applies specific comparison rules based on the types involved:
  • Numeric vs. Numeric (Integer/Float): Performs a standard mathematical comparison.
  • String vs. String: If both strings are well-formed numeric strings, PHP casts them to numbers and performs a mathematical comparison. Otherwise, it performs a lexicographical (alphabetical) comparison based on the underlying byte (ASCII) values of the characters.
  • Numeric vs. String:
    • PHP 8.0 and later: If the string contains a well-formed numeric value, it is cast to a number for mathematical comparison. If the string is non-numeric, the number is cast to a string, and a lexicographical comparison is performed.
    • Prior to PHP 8.0: The string is aggressively cast to a number (often evaluating to 0 if non-numeric) before comparison.
  • Boolean vs. Any Type: The non-boolean operand is cast to a boolean before comparison. In PHP, false is considered less than true.
  • Array vs. Array: Evaluates the count of elements. An array with more elements is considered greater. If the counts are identical, PHP recursively compares the values of the elements key by key.

Syntax and Evaluation Examples

// Standard numeric comparison
var_dump(10 > 5);        // bool(true)
var_dump(5 > 5);         // bool(false) - strict inequality required

// String comparison (Numeric strings vs. Lexicographical)
var_dump("10" > "2");    // bool(true) - Both are numeric strings, compared mathematically as 10 > 2
var_dump("z" > "a");     // bool(true) - Lexicographical: ASCII 122 > ASCII 97
var_dump("10a" > "2b");  // bool(false) - Non-numeric strings, lexicographical: '1' (49) is not > '2' (50)

// Cross-type coercion (PHP 8.0+ behavior)
var_dump(42 > "20");     // bool(true) - "20" is cast to integer 20
var_dump(42 > "foo");    // bool(false) - 42 cast to "42", lexicographical comparison fails

// Boolean coercion
var_dump(true > false);  // bool(true) - true is greater than false
var_dump(5 > true);      // bool(false) - 5 is cast to true, evaluates as true > true

Operator Precedence and Associativity

The > operator has non-associative directionality. It sits lower in the precedence table than arithmetic operators (+, -, *, /) but higher than logical operators (&&, ||) and assignment operators (=). Because it is non-associative, chaining the operator without explicit grouping parentheses results in a parse error.
// Valid: Arithmetic evaluates before the relational operator
$result = 5 + 5 > 8;     // Evaluates as (5 + 5) > 8 -> bool(true)

// Invalid: Non-associative chaining throws a ParseError
$result = 10 > 5 > 2;    // Parse error: syntax error, unexpected token ">"

// Valid: Explicit grouping resolves associativity
$result = (10 > 5) > 2;  // Evaluates as true > 2. 
                         // 2 is cast to boolean true. 
                         // Evaluates as true > true -> bool(false)
Master PHP with Deep Grasping Methodology!Learn More