The <=> ("Spaceship") operator will offer combined comparison in that it will :

Return 0 if values on either side are equal
Return 1 if the value on the left is greater
Return -1 if the value on the right is greater

The rules used by the combined comparison operator are the same as the currently used comparison operators by PHP viz. <, <=, ==, >= and >. Those who are from Perl or Ruby programming background may already be familiar with this new operator proposed for PHP7.

   //Comparing Integers

    echo 1 <=> 1; //output  0
    echo 3 <=> 4; //output -1
    echo 4 <=> 3; //output  1
    
    //String Comparison
    
    echo "x" <=> "x"; //output  0
    echo "x" <=> "y"; //output -1
    echo "y" <=> "x"; //output  1

When you compare a string this way, it will go from left to right, and compare each character in the given string to see if they are different, until it finds a difference, then it will decide which string is bigger by comparing the ASCII value of this last character.

Answer from GreenROBO on Stack Overflow
🌐
Tutorialspoint
tutorialspoint.com › home › php › php spaceship operator
PHP Spaceship Operator
May 26, 2007 - Explore the PHP spaceship operator with detailed examples and explanations to improve your coding proficiency.
Top answer
1 of 3
378

The <=> ("Spaceship") operator will offer combined comparison in that it will :

Return 0 if values on either side are equal
Return 1 if the value on the left is greater
Return -1 if the value on the right is greater

The rules used by the combined comparison operator are the same as the currently used comparison operators by PHP viz. <, <=, ==, >= and >. Those who are from Perl or Ruby programming background may already be familiar with this new operator proposed for PHP7.

   //Comparing Integers

    echo 1 <=> 1; //output  0
    echo 3 <=> 4; //output -1
    echo 4 <=> 3; //output  1
    
    //String Comparison
    
    echo "x" <=> "x"; //output  0
    echo "x" <=> "y"; //output -1
    echo "y" <=> "x"; //output  1

When you compare a string this way, it will go from left to right, and compare each character in the given string to see if they are different, until it finds a difference, then it will decide which string is bigger by comparing the ASCII value of this last character.

2 of 3
97

According to the RFC that introduced the operator, $a <=> $b evaluates to:

  • 0 if b
  • -1 if b
  • 1 if b

which seems to be the case in practice in every scenario I've tried, although strictly the official docs only offer the slightly weaker guarantee that $a <=> $b will return

an integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b

Regardless, why would you want such an operator? Again, the RFC addresses this - it's pretty much entirely to make it more convenient to write comparison functions for usort (and the similar uasort and uksort).

usort takes an array to sort as its first argument, and a user-defined comparison function as its second argument. It uses that comparison function to determine which of a pair of elements from the array is greater. The comparison function needs to return:

an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

The spaceship operator makes this succinct and convenient:

$things = [
    [
        'foo' => 5.5,
        'bar' => 'abc'
    ],
    [
        'foo' => 7.7,
        'bar' => 'xyz'
    ],
    [
        'foo' => 2.2,
        'bar' => 'efg'
    ]
];

// Sort $things by 'foo' property, ascending
usort($things, function (b) {
    return b['foo'];
});

// Sort $things by 'bar' property, descending
usort($things, function (b) {
    return a['bar'];
});

More examples of comparison functions written using the spaceship operator can be found in the Usefulness section of the RFC.

🌐
GeeksforGeeks
geeksforgeeks.org › php › php-7-spaceship-operator
PHP 7 | Spaceship Operator - GeeksforGeeks
July 11, 2025 - This <=> operator offers combined comparison : ... // Comparing Integers echo 1 <=> 1; // outputs 0 echo 3 <=> 4; // outputs -1 echo 4 <=> 3; // outputs 1 // String Comparison echo "a" <=> "a"; // outputs 0 echo "m" <=> "y"; // outputs -1 echo "y" <=> "c"; // outputs 1 ... <?php echo"Integers \n"; echo 7 <=> 7 ; echo"\n"; echo 7 <=> 6; echo"\n"; echo 6 <=> 7; echo"\nFloat\n"; echo 2.5 <=> 1.5; echo"\n"; echo 0.5 <=> 1.5; echo"\n"; echo 1.5 <=> 1.5; echo"\nStrings\n"; echo "a" <=> "a" ; echo"\n"; echo "g" <=> "b" ; echo"\n"; echo "a" <=> "b" ; echo"\nArrays\n"; echo [] <=> []; echo"\n"; echo [1, 7, 3] <=> [1, 7, 3]; echo"\n"; echo [1, 7, 3, 5] <=> [1, 7, 3]; echo"\n"; echo [1, 7, 3] <=> [4, 4, 4]; echo"\n"; ?> Output:
🌐
W3schools
w3schools.tech › tutorial › php › php_spaceship_operator
PHP - Spaceship Operator - PHP Operators - W3schools
Now, let's see this operator in action with some examples! Let's start with something simple – comparing numbers. <?php $result1 = 5 <=> 10; echo "5 <=> 10 = $result1\n"; $result2 = 10 <=> 5; echo "10 <=> 5 = $result2\n"; $result3 = 5 <=> 5; echo "5 <=> 5 = $result3\n"; ?> ... See how quickly we can determine the relationship between these numbers? It's like having a mini math wizard in our code! The Spaceship Operator isn't just for numbers – it works with strings too!
🌐
PHP
php.net › manual › en › migration70.new-features.php
PHP: New features - Manual
$username = $_GET['user'] ?? 'nobody'; ... = $_GET['user'] ?? $_POST['user'] ?? 'nobody'; ?> The spaceship operator is used for comparing two expressions....
🌐
Medium
medium.com › @nabilhasan.live › spaceship-operator-in-php-eec288fec5c6
Spaceship operator in PHP. After two years working in PHP/Laravel… | by Nabil Hasan | Medium
August 5, 2023 - <?php $a = 10; $b = 5; if ($a > $b) { $result = 1; } elseif ($a < $b) { $result = -1; } else { $result = 0; } echo $result; ?> Here we have to use multiple if else conditions however using “<=>” spaceship operator we can achieve that within one line.
🌐
Exakat
php-dictionary.readthedocs.io › en › latest › dictionary › spaceship.ini.html
Spaceship Operator — PHP Dictionary 1.0.0 documentation
3 weeks ago - Spaceship Operator: Spaceship operator is an operator which reports if a value is larger, equal or smaller than another value.
🌐
FlatCoding
flatcoding.com › home › php spaceship operator
PHP Spaceship Operator - FlatCoding
April 15, 2025 - The Spaceship Operator in PHP uses the ASCII value of each character if you are working with strings. For example, "apple" <=> "banana" would come out as -1 because “apple” comes before “banana” in alphabetical order.
Find elsewhere
🌐
Rip Tutorial
riptutorial.com › spaceship operator ( · )
PHP Tutorial => Spaceship Operator (<=>)
This operator is particularly useful when writing a user-defined comparison function using usort, uasort, or uksort. Given an array of objects to be sorted by their weight property, for example, an anonymous function can use <=> to return the value expected by the sorting functions. usort($list, function($a, $b) { return $a->weight <=> $b->weight; }); In PHP 5 this would have required a rather more elaborate expression.
🌐
Designcise
designcise.com › web › tutorial › what-is-the-php-spaceship-operator
What Is the PHP Spaceship Operator (<=>)?
August 13, 2020 - 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 ...
🌐
PHP
wiki.php.net › rfc › combined-comparison-operator
PHP RFC: Combined Comparison (Spaceship) Operator
With this operator, you can easily write proper ordering functions, like this one: ... function order_func($a, $b) { return ($a->$x <=> $b->x) ?: ($a->y <=> $b->y) ?: ($a->foo <=> $b->foo); } It is also useful in some other contexts. This introduces no backwards incompatible changes. The next major version of PHP, currently PHP 7.0. A T_SPACESHIP constant for use with ext/tokenizer has been added.
🌐
Medium
medium.com › @Amir_M4A › the-spaceship-operator-in-php-a-powerful-tool-for-comparison-functions-1101ff14c8e6
The Spaceship Operator in PHP: A Powerful Tool for Comparison Functions | by Amir | Medium
September 24, 2024 - Learn how to efficiently sort arrays in PHP using the powerful spaceship operator ( ). Clear examples for beginners to improve code readability and speed!"
🌐
Rip Tutorial
riptutorial.com › spaceship operator
php-7 Tutorial => Spaceship operator
Learn php-7 - The spaceship operator is used for comparing two expressions. For example, $a $b returns -1, 0 or 1 when $a is respectively less...
🌐
W3Schools
w3schools.com › php › php_operators.asp
PHP Operators
PHP If PHP If Operators PHP If...Else PHP Shorthand if PHP Nested if PHP Switch PHP Match PHP Loops
🌐
DEV Community
dev.to › thibaultchatelain › spaceship-operator-in-php-5g0n
Spaceship operator 🚀 in PHP - DEV Community
July 16, 2024 - Introduced in PHP 7, the Spaceship operator <=> is used to compare two expressions. It... Tagged with php, operator.
🌐
Amit Merchant
amitmerchant.com › remembering-what-spaceship-operator-do-comparison-php
Remembering what spaceship operator do on comparison in PHP
February 20, 2020 - What you’d do is to replace the spaceship operator (<=>) with a minus sign (-). If the result is negative, 0 or positive, the expression will return -1, 0 or 1 respectively. As you can see, it’s now more easy to remember when an analogy comes into play. …And that is also how you remember anything you want, in general. Previous: Using register_shutdown_function() instead of desctructor in PHP Next: Using whereNull and whereNotNull in Eloquent Collection in Laravel
🌐
ZetCode
zetcode.com › php › spaceship-operator
PHP Spaceship Operator - Comparing Values
PHP Spaceship Operator tutorial explains the operator for comparisons, with examples on sorting and custom objects.
🌐
Franktheprogrammer
franktheprogrammer.com › blog › php › php-spaceship-operator
PHP Spaceship Operator
One of the new features to hit PHP 7 is the Spaceship Operator. This new trick helps improve the way you’d compare 2 expressions. In short, the comparison returns 1 of 3 values (-1, 0, or 1) depending on the result of the comparison.