What is <=> (the 'Spaceship' Operator) in PHP 7?
Solution 1
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
Solution 2
According to the RFC that introduced the operator, $a <=> $b
evaluates to:
- 0 if
$a == $b
- -1 if
$a < $b
- 1 if
$a > $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 ($a, $b) {
return $a['foo'] <=> $b['foo'];
});
// Sort $things by 'bar' property, descending
usort($things, function ($a, $b) {
return $b['bar'] <=> $a['bar'];
});
More examples of comparison functions written using the spaceship operator can be found in the Usefulness section of the RFC.
Solution 3
Its a new operator for combined comparison. Similar to strcmp()
or version_compare() in behavior, but it can be used on all generic PHP values with the same semantics as <
, <=
, ==
, >=
, >
. It returns 0
if both operands are equal, 1
if the left is greater, and -1
if the right is greater. It uses exactly the same comparison rules as used by our existing comparison operators: <
, <=
, ==
, >=
and >
.
Related videos on Youtube
Deepak Mankotia
India-based full-stack developer. Currently working with : - #react | #redux | #relay | #grapql | #php | #.net-core | #python #angular | #javascript Exploring : #AI | #ML | #Blockchain Organizer GraphQLCHD : Twitter | Slack | LinkedIN | TownScript Feel free to contact me at [email protected] My Blogs - what is closure in javascript, advantages and disadvantages? | What is event loop in javascript? | Understanding React Hooks | enter link description here If you find something wrong in one of my posts, feel free to edit it. I'm a frequent visitor and can always roll back if I think a change you've made is wrong or stupid, so you may as well be bold - it's better, here, to ask for forgiveness than permission.
Updated on July 26, 2022Comments
-
Deepak Mankotia over 1 year
PHP 7, which will come out in November this year will introduce the Spaceship (<=>) operator. What is it and how does it work?
This question already has an answer in our general reference question about PHP operators.
-
pavel almost 9 yearsCurrent PHP version is PHP 5.x, PHP 7 is maybe a draft and can be changed many times in future.
-
Cactus almost 9 years
-
M.P. Korstanje almost 9 years
-
Admin almost 9 years@panther PHP 7 is real, and the first Release Candidates are due within weeks - although it remains to be seen if that timetable will be met. You can see the planned timeline here
-
userfuser over 8 years@pala_, to google something, the Google must first "google it" and find the "answer" somewhere. I googled it and found this page and I'm ok with this question posted.
-
-
Altaf Hussain about 8 yearsBut how the strings are compared? Is it looking into characters, count of characters in strings and so on?
-
Krzysztof Karski almost 8 years@AltafHussain see this answer: (stackoverflow.com/a/17371819/4337069)
-
Viju over 5 yearsSpaceship Operator is used to compare two expressions. It returns -1, 0 or 1 when first expression is respectively less than, equal to, or greater than second expression. nexladder.com/php7-tutorial/php7-spaceship-operator
-
jave.web over 5 yearsTo me, question "what is" is not just "what does it do", but also "what is it for, where can it be seen", therefore I consider this the right and complete answer :)
-
Raju over 5 yearsIt can be mostly used in sorting and combined comparison.