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 <=> (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.
$result = $expr1 <=> $expr2;

Return Value Mechanics

The operator evaluates the operands and strictly returns one of three integer values:
  • 0 if $expr1 is equal to $expr2 ($expr1 == $expr2)
  • -1 if $expr1 is less than $expr2 ($expr1 < $expr2)
  • 1 if $expr1 is 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.
echo 10 <=> 10;   // 0
echo 5 <=> 10;    // -1
echo 10 <=> 5;    // 1

echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
Strings Strings are evaluated using lexicographical order based on their ASCII/byte values. Case sensitivity applies, with uppercase letters having lower ASCII values than lowercase letters.
echo "x" <=> "x"; // 0
echo "x" <=> "y"; // -1
echo "y" <=> "x"; // 1
echo "Z" <=> "a"; // -1 
Mixed Types (Strings and Numbers) When comparing a string to a number, PHP’s behavior depends on the string’s contents and the PHP version. In PHP 8.0 and later (“Saner string to number comparisons”), if the string is non-numeric, the number is cast to a string and compared lexicographically.
echo "10" <=> 10;        // 0 (Numeric string, compared as integers)
echo "5" <=> 10;         // -1 (Numeric string, compared as integers)

// PHP 8.0+ Semantics:
// 10 is cast to "10". "10 apples" is lexicographically greater than "10".
echo "10 apples" <=> 10; // 1 
Booleans Boolean comparison treats false as less than true.
echo false <=> false; // 0
echo false <=> true;  // -1
echo true <=> false;  // 1
Null null evaluates as equal to null, false, and empty strings (""), following standard type juggling rules.
echo null <=> null;   // 0
echo null <=> false;  // 0
echo null <=> true;   // -1
echo null <=> "";     // 0
Arrays Array comparison evaluates sequentially: it first compares the count of elements, then the keys, and finally the values element by element.
// Equal count and values
echo [1, 2] <=> [1, 2];       // 0

// Left array has fewer elements
echo [1, 2] <=> [1, 2, 3];    // -1

// Left array has more elements
echo [1, 2, 3] <=> [1, 2];    // 1

// Equal count, but left array has a lesser value at index 1
echo [1, 2, 3] <=> [1, 3, 3]; // -1
Objects When comparing objects of the same class, the operator recursively compares their properties based on the order they were declared.
$obj1 = (object) ['x' => 1, 'y' => 2];
$obj2 = (object) ['x' => 1, 'y' => 2];
$obj3 = (object) ['x' => 1, 'y' => 5];

echo $obj1 <=> $obj2; // 0 (Equal properties)
echo $obj1 <=> $obj3; // -1 (Property 'y' is 2 vs 5)
Master PHP with Deep Grasping Methodology!Learn More