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.

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

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
May 3, 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 10, 2014
Use case for Anonymous classes and <=> (the 'Spaceship' Operator) in PHP
Anonymous classed are good for unit testing abstract classes More on reddit.com
🌐 r/PHP
23
47
November 11, 2016
🌐
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.
🌐
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.
🌐
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.
🌐
W3Schools
w3schools.com › php › php_operators.asp
PHP Operators
PHP divides the operators in the following groups:
🌐
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.