<=> (spaceship) operator, formally known as the three-way comparison operator, evaluates two expressions and returns an integer representing their relative order. Introduced in PHP 7.0, it performs a combined less-than, equal-to, and greater-than comparison in a single operation.
Return Value Mechanics
The operator evaluates the operands and strictly returns one of three integer values:0if$expr1is equal to$expr2($expr1 == $expr2)-1if$expr1is less than$expr2($expr1 < $expr2)1if$expr1is greater than$expr2($expr1 > $expr2)
Evaluation Rules and Type Coercion
The<=> operator relies on PHP’s standard loose comparison rules. It does not perform strict type checking (like ===); instead, it applies type coercion when comparing operands of different data types.
Associativity: The operator is non-associative. Chaining the operator (e.g., $a <=> $b <=> $c) will result in a fatal parse error.
Syntax Visualization by Data Type
Integers and Floats Numeric comparison is evaluated by standard mathematical value.false as less than true.
null evaluates as equal to null, false, and empty strings (""), following standard type juggling rules.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More





