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
๐ŸŒ
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 ...
๐ŸŒ
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.
Discussions

What is (the 'Spaceship' Operator) in PHP 7? - Stack Overflow
PHP 7 introduced the Spaceship ( ) operator. What is it and how does it work? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why <=> (the 'Spaceship' Operator) ?
It is not about the speed, it's about the cleaner code. Like many other syntax sugar-like improvements, it is not intended to make your code run as fast as a space ship, but just to make it a little bit more concise. For example, I hope noone have an idea that a short syntax for arrays, [] is "faster" than old array(). It's just a syntax sugar. SAME HERE. More on reddit.com
๐ŸŒ r/PHPhelp
5
2
October 3, 2017
Shorthand comparisons in PHP, now also looking at the spaceship operator
I'll smack you if I see you using chaining/nested ternary. More on reddit.com
๐ŸŒ r/PHP
37
76
April 28, 2018
Interesting use of the spaceship operator

Or you could write readable code. That's also an option.

(not that this isn't neat)

More on reddit.com
๐ŸŒ r/PHP
19
8
September 6, 2014
๐ŸŒ
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.
๐ŸŒ
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.
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.

๐ŸŒ
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....
Find elsewhere
๐ŸŒ
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 useful when sorting arrays or for comparing two values without the need for multiple conditional statements. It simplifies the code and makes it more concise. <?php $a = 10; $b = 5; $result = $a <=> $b; echo $result ?>
๐ŸŒ
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 - The spaceship operator (<=>) is a powerful and beginner-friendly tool that can transform how you write comparison functions in PHP. With its concise syntax and efficient performance, youโ€™ll find yourself writing cleaner, faster, and more ...
๐ŸŒ
Koenwoortman
koenwoortman.com โ€บ php-spaceship-operator
PHP Spaceship Operator (<=>)
The spaceship operator evaluates to either -1, 0 or 1. Let's consider two operarands a and b being compared by the spaceship operator: a <=> b.
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-7-spaceship-operator
PHP 7 | Spaceship Operator - GeeksforGeeks
July 11, 2025 - This article will make you aware of a very useful operator i.e the spaceship operator PHP 7. The spaceship operator or combined comparison operator is denoted by "<=>". This is a three-way comparison operator and it can perform greater than, ...
๐ŸŒ
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().
๐ŸŒ
ZetCode
zetcode.com โ€บ php โ€บ spaceship-operator
PHP Spaceship Operator - Comparing Values
Introduced in PHP 7, the spaceship operator <=> compares two values, returning -1, 0, or 1 if the left is less than, equal to, or greater than the right. It simplifies sorting and comparisons, unifying numeric, string, and mixed-type logic.
๐ŸŒ
DEV Community
dev.to โ€บ thicha0 โ€บ 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.
๐ŸŒ
Medium
medium.com โ€บ @dabersamir โ€บ php-spaceship-operator-what-it-is-and-how-it-works-2748b2fa9da5
PHP Spaceship Operator: What It Is and How It Works | by Dabersamir | Medium
December 24, 2024 - Letโ€™s dive in and see why this quirky-named operator is such a game-changer (oops, I meant a useful tool). The PHP spaceship operator (<=>) is a comparison operator in PHP that simplifies how we compare two values.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_operators.asp
PHP Operators
PHP divides the operators in the following groups: