🌐
Tutorialspoint
tutorialspoint.com › home › php › php spaceship operator
PHP Spaceship Operator
May 26, 2007 - Learn about the PHP spaceship operator, its syntax, usage, and examples to enhance your programming skills.
single computing operation which compares two values and returns their relative order
In computer science, a three-way comparison takes two values A and B belonging to a type with a total order and determines whether A < B, A = B, or A > … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Three-way_comparison
Three-way comparison - Wikipedia
October 30, 2025 - In Perl (for numeric comparisons only, the cmp operator is used for string lexical comparisons), PHP (since version 7), Ruby, and Apache Groovy, the "spaceship operator" <=> returns the values −1, 0, or 1 depending on whether A < B, A = B, or A > B, respectively.
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.

🌐
YouTube
youtube.com › trail six
PHP Spaceship Operator - YouTube
PHP, HTML, CSS, JavaScript, Python, AI/ML and related tutorials. This time I'll show you the PHP Spaceship Operator, which is new in PHP7.Like the channel? C...
Published   May 29, 2020
Views   116
🌐
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, ...
🌐
Delft Stack
delftstack.com › home › howto › php › php spaceship operator
PHP Spaceship Operator | Delft Stack
September 22, 2022 - The Spaceship Operator with Integer Values: 0 -1 1 The Spaceship Operator with Float Values: 0 -1 1 The Spaceship Operator with String Values: 0 -1 1 The Spaceship Operator with Arrays: 0 0 1 -1 · Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe ... Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
🌐
PHP
wiki.php.net › rfc › combined-comparison-operator
PHP RFC: Combined Comparison (Spaceship) Operator
Add a new operator (expr) <=> (expr), it returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater.
🌐
Designcise
designcise.com › web › tutorial › what-is-the-php-spaceship-operator
What Is the PHP Spaceship Operator (<=>)?
August 13, 2020 - 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.
🌐
Hashnode
codeweb.hashnode.dev › the-complete-guide-to-php-spaceship-operator
The Complete Guide to PHP Spaceship Operator - Code Web
November 30, 2022 - <?php operand_1 || operand_2 // => or operand_1 or operand_2 ?> So when you use it with if condit... ... Get new posts delivered to your inbox. In this tutorial you will learn about what react is and how it does work, also you will learn how to write your first app with react.js Let's understand, what is react? and how it does work. An Overview React.js is a library created by software engi... ... In this tutorial, you will understand what python is, and how the online python compiler works, also you will understand how the CPython interpreter works.
Find elsewhere
🌐
Onlinestudy24
onlinestudy24.com › php › php_Spaceship.html
PHP Spaceship Operator (<=>) - Complete Guide with Examples
October 15, 2023 - Swift Tutorial PHP Tutorial JavaScript ... spaceship operator (<=>), introduced in PHP 7, is a three-way comparison operator that offers a concise way to compare two expressions....
🌐
Reddit
reddit.com › r/php › shorthand comparisons in php, now also looking at the spaceship operator
r/PHP on Reddit: Shorthand comparisons in PHP, now also looking at the spaceship operator
May 3, 2018 - In a not-too-distant future php will hopefully get a null safe navigation operator. ... Thanks but no magic one liners please. Less lines does not maketh better code. More lines with clear structure and comments are much better. ... The null coalescing and spaceship operator (three way comparison) have been available in many languages for a long time and are meant to express very fundamental concepts.
🌐
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 may be replace by a - (minus) sign, if the compared values are correct.
🌐
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 - In PHP, the “<=>” operator is referred to as the “spaceship operator” or “three-way comparison operator.” It was first used in PHP 7 to compare two expressions and return a value based on their relationship.
🌐
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 ...
🌐
FlatCoding
flatcoding.com › home › php spaceship operator
PHP Spaceship Operator - FlatCoding
April 15, 2025 - Imagine you have to sort a list—scores ranking or name sorting. With the Spaceship Operator, you needn’t write more lines of code; it just does it. This clarity makes it especially handy for sorting. Below is how the operator is used, with examples. Here’s how the PHP Spaceship Operator looks in action:
🌐
ZetCode
zetcode.com › php › spaceship-operator
PHP Spaceship Operator - Comparing Values
SQLite Python · wxPython ebook ... March 20, 2025 · 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....
🌐
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().
🌐
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.