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
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.

🌐
Tutorialspoint
tutorialspoint.com › home › php › php spaceship operator
PHP Spaceship Operator
May 26, 2007 - The following example shows how you can use the spaceship operator in PHP. We will compare 5 with 10/2.
🌐
PHP
php.net › manual › en › migration70.new-features.php
PHP: New features - Manual
$username = $_GET['user'] ?? 'nobody'; // This is equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; // Coalescing can be chained: this will return the first // defined value out of $_GET['user'], $_POST['user'], and ...
🌐
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 - The spaceship operator is particularly ... statements. It simplifies the code and makes it more concise. <?php $a = 10; $b = 5; $result = $a <=> $b; echo $result ?> In this example, $result will be 1 because $a is greater than ...
🌐
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:
🌐
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.
🌐
FlatCoding
flatcoding.com › home › php spaceship operator
PHP Spaceship Operator - FlatCoding
April 15, 2025 - PHP’s spaceship operator looks at the ASCII values of strings when it compares them. ASCII is a system where each letter, number, and symbol is assigned a number so the computer can work with them. So, when you are using the spaceship operator to compare strings, it’s actually checking the ASCII value for each character in the string. In ASCII, for example, the lowercase letter ‘a’ has a lower value than the letter ‘b’. Hence, when comparing strings, the operator goes character by character, with the comparison based on the ASCII value of each.
🌐
Rip Tutorial
riptutorial.com › spaceship operator ( · )
PHP Tutorial => Spaceship Operator (<=>)
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 ...
Find elsewhere
🌐
Designcise
designcise.com › web › tutorial › what-is-the-php-spaceship-operator
What Is the PHP Spaceship Operator (<=>)?
August 13, 2020 - It has the following syntax: (expr) ... following values: If $a is less than $b then the result would be -1; If $a is equal to $b then the result would be 0; If $a is greater than $b then the result would be 1. The comparisons between ...
🌐
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!"
🌐
Franktheprogrammer
franktheprogrammer.com › blog › php › php-spaceship-operator
PHP Spaceship Operator
Lastly, we have some examples where the first value is greater than the second value generating a positive 1 value. <?php echo 2 <=> 1; echo 2.5 <=> 1.5; echo "b" <=> "a";
🌐
Virendra's TechTalk
virendrachandak.com › techtalk › php › php 7 – combined comparison (spaceship) operator
PHP 7 - Combined Comparison (Spaceship) Operator - Virendra's TechTalk
March 26, 2016 - The spaceship operator (<=>) returns -1 if the left side is smaller, 0 if the values are equal and 1 if the left side is larger. It can be used on all generic PHP values with the same semantics as < , <=, ==, >=, >. This operator is similar in behavior to strcmp() or version_compare(). This ...
🌐
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.
🌐
DevTut
devtut.github.io › php › spaceship-operator.html
PHP - Spaceship operator
The spaceship operator is used for comparing two expressions. For example, $a <=> $b returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b. Comparisons are performed according to PHP's usual type comparison rules.
🌐
Amit Merchant
amitmerchant.com › remembering-what-spaceship-operator-do-comparison-php
Remembering what spaceship operator do on comparison in PHP
February 20, 2020 - What this spaceship operator do is compare two expressions i.e. its two operands, let’s say $a and $b, and returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b.
🌐
DEV Community
dev.to › rezende79 › spaceship-ternary-and-null-coalescing-operators-in-php-quick-examples-13l7
Spaceship, Ternary and Null Coalescing operators in PHP: Quick examples - DEV Community
August 14, 2021 - Ho to compare two values in PHP guided by quick examples of Spaceship, Ternary and Null Coalescing operators. Tagged with php, comparison, operator, tutorial.
🌐
Rip Tutorial
riptutorial.com › spaceship operator
php-7 Tutorial => Spaceship operator
The spaceship operator is used for comparing two expressions. For example, $a <=> $b returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b. Comparisons are performed according to PHP's usual type comparison rules.
🌐
CraftQuest
craftquest.io › homepage › using the spaceship operator in twig
Using the Spaceship Operator in Twig | CraftQuest
January 21, 2022 - A typ­i­cal exam­ple of using the space­ship oper­a­tor is for sort­ing. In Twig we have the sort fil­ter, which uses PHP’s asort func­tion for sort­ing arrays.
🌐
Programster
blog.programster.org › php-7-spaceship-operator
PHP 7 - Spaceship Operator | Programster's Blog
September 16, 2021 - This operator is perfect for custom sort functions with usort, uksort, and uasort. For example, if I wanted to write a custom sort function before this came along, I would write something like:
🌐
Exakat
php-dictionary.readthedocs.io › en › latest › dictionary › spaceship.ini.html
Spaceship Operator — PHP Dictionary 1.0.0 documentation
3 weeks ago - The spaceship operator got its name from its resemblance to an flying saucer. Battleship operator, or death star operator do not exist. ... See also https://www.exakat.io/en/weird-operators-in-php/, https://www.designcise.com/web/tutorial/what-is-the-php-spaceship-operator