What is the purpose of the "Spaceship Operator" <=> of PHP7?

Asked

Viewed 2,497 times

20

I was just taking a look at the New Features of PHP 7 and I came across an operator, who I had never seen in any programming language. The PHP Handbook demonized him of Spaceship Operator.

I’ll demonstrate them below what I saw:

echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

I don’t know if I got it right, but it seems to me he does the same thing as strcmp.

  • After all, simply, what is the purpose of this operator?
  • What are the advantages gained by using it?
  • 1

    I was waiting for that question :D

  • And you were hoping I’d ask?

5 answers

26


The operator <=> is used to make combined comparisons.

  • Returns 0 if the values on both sides are equal.
  • Returns 1 if the value to left is larger.
  • Returns -1 if the value to right is larger.

Example:

echo 1 <=> 1; // 0
echo 3 <=> 4; // -1
echo 4 <=> 3; // 1

The advantage in using the operator <=> is that it is not restricted to a particular type, while the function strcmp is limited to strings.

In languages such as Ruby, Perl and Groovy that operator is also present.

  • It’ll be nice to see it working with arrays. It will change the life of array_sort and the like :)

10

The documentation already says well what it is:

The operator Spaceship is used to compare two expressions. It returns an integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Comparisons are made according to the usual PHP type comparison rules.

Improving the example of documentation:

echo -1 <=> -1; // 0
echo -10 <=> 2; // -1
echo 20 <=> 1; // 1

The main advantage is to be independent of types.

  • 3

    +1 for Improving the example of documentation. That example implied that he could return -2, -5. Not because of the explanation, but because of the code - which is the first thing I look at, when I don’t understand the explanation.

6

From one version here, php seems to be trading some functions for operators. For example, Pow() that makes the exponentiation calculation can be exchanged for (**); func_get_args() can be exchanged for Spread Operator. These two are available from the php 5.6.

The spacecraft does almost the same thing as the function strcmp, however applies to other types, and not only to strings.

  • Allow me to add that ** is exponentiation operator. I like to know the names of these guys.

  • @Wallacemaxters ** it’s bold, I didn’t know if it would come out normal, so I said two astericas haha.

5

In general languages use a function. The function you mentioned compares strings, already the operator <=> works with various types where this form of comparison is possible.

Before PHP even had a function that did this with numbers.

3

Complementing the @stderr response, you can use it in functions that work with data sorting.

See the case of the usort function, which takes as the second parameter a function that must return -1, 0 or 1 as a result of the ordering of 02 parameters received.

usort($durations, function ($a, $b) {
    return $a <=> $b;
});

That’s the same as

usort($durations, function ($a, $b) {
if($a < $b) {
    return -1;
}
if($a > $b) {
    return 1;
}

return 0;
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.