What Is the PHP Spaceship Operator?

The spaceship (or three-way comparison) operator (<=>), introduced in PHP 7.0, is used for comparing expressions. It has the following syntax:

(expr) <=> (expr)

Rules of Comparison

The result of comparison, for example between two values $a and $b, would yield one of the following values:

  1. If $a is less than $b then the result would be -1;
  2. If $a is equal to $b then the result would be 0;
  3. If $a is greater than $b then the result would be 1.

The comparisons between different types are performed based on PHP's type comparison rules.

The spaceship operator uses loose equality (i.e. ==) for equality comparisons. This means that $a and $b are considered equal when they:

  • Have the same value after type juggling;
  • Have the same key/value pairs in case of arrays (regardless of their type and/or order).

Spaceship Operator (<=>) vs. Other Comparison Operators

The following table shows how the spaceship operator compares to the other comparison operators in PHP:

Operator <=> Equivalent
$a < $b ($a <=> $b) === -1
$a <= $b ($a <=> $b) === -1 || ($a <=> $b) === 0
$a == $b ($a <=> $b) === 0
$a != $b ($a <=> $b) !== 0
$a >= $b ($a <=> $b) === 1 || ($a <=> $b) === 0
$a > $b ($a <=> $b) === 1

Supported Data Types

The spaceship operator supports all the data types that you would use in normal comparison operators (such as integers, floats, strings, arrays, objects, etc.). You can find below some examples of using the spaceship operator with different data types:

Comparing Integers:

echo 1 <=> 2; // -1
echo 2 <=> 2; // 0
echo 2 <=> 1; // 1

Comparing Strings:

echo 'x' <=> 'y'; // -1
echo 'y' <=> 'y'; // 0
echo 'y' <=> 'x'; // 1

Comparing Floats:

echo 0.5 <=> 1.5; // -1
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 0.5; // 1

Comparing Arrays:

echo [] <=> [1, 2, 3]; // -1
echo [1, 2, 3] <=> [3, 2, 1]; // -1

echo [] <=> []; // 0
echo [1, 2, 3] <=> [1, 2, 3]; // 0

echo [3, 2, 1] <=> [1, 2, 3]; // 1
echo [1, 2, 3] <=> []; // 1

Comparing Objects:

echo (object) ['x' => 'y'] <=> (object) ['x' => 'z']; // -1
echo (object) ['x' => 'y'] <=> (object) ['x' => 'y']; // 0
echo (object) ['x' => 'z'] <=> (object) ['x' => 'y']; // 1

What Is the Spaceship Operator Useful For?

It was meant as more of a convenience operator to make it easier to write comparison functions for sorting functions such as usort, uasort, etc. For example:

$products = [
    ['name' => 'Bag' , 'price' => 49.99],
    ['name' => 'Belt' , 'price' => 34.99],
    ['name' => 'Shoes' , 'price' => 50],
    ['name' => 'Shirt' , 'price' => 34.99],
];

$comparisonFn = function (array $a, array $b) {
    return $a['price'] <=> $b['price'];
};

usort($products, $comparisonFn);

var_dump($products);

/* array(4) {
    [0]=> array(2) { ["name"]=> string(4) "Belt" ["price"]=> float(34.99) }
    [1]=> array(2) { ["name"]=> string(5) "Shirt" ["price"]=> float(34.99) }
    [2]=> array(2) { ["name"]=> string(3) "Bag" ["price"]=> float(49.99) }
    [3]=> array(2) { ["name"]=> string(5) "Shoes" ["price"]=> int(50) }
} */

Without the spaceship operator the comparison function would look something like this:

$comparisonFn = function (array $a, array $b) {
    if ($a['price'] == $b['price']) {
        return 0;
    }
    return ($a['price'] < $b['price']) ? -1 : 1;
};

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.