1

I am pretty new to PHP7 and so far it seems great and powerful. I have been using PHP5.6 so I started understanding the usage of spaceship operator <=>. But somehow I couldn't get the logic that statement returns -1. I know the point of returning to 0 or 1 which are false or true. Can anyone clarify the usage of return -1?

Function normal_sort($a, $b) : int 
{ 
  if( $a == $b )
    return 0;
  if( $a < $b )
   return -1;
  return 1;
}

function space_sort($a, $b) : int
{
   return $a <=> $b;
}

$normalArray = [1,34,56,67,98,45];

//Sort the array in asc
usort($normalArray, 'normal_sort');

foreach($normalArray as $k => $v)
{
   echo $k.' => '.$v.'<br>';
}

$spaceArray = [1,34,56,67,98,45];

//Sort it by spaceship operator
usort($spaceArray, 'space_sort');

foreach($spaceArray as $key => $value)
{
   echo $key.' => '.$value.'<br>';
}
AbraCadaver
  • 73,820
  • 7
  • 55
  • 81
Mr.T
  • 67
  • 1
  • 9
  • 2
    Possible duplicate of [What is <=> (the 'Spaceship' Operator) in PHP 7?](http://stackoverflow.com/questions/30365346/what-is-the-spaceship-operator-in-php-7) – HPierce May 18 '16 at 20:01
  • This answer on the question I marked answers that: http://stackoverflow.com/a/32802797/3000068 – HPierce May 18 '16 at 20:02
  • I saw them. My question is not precisely what spaceship operator does. What is the meaning of returning -1? – Mr.T May 18 '16 at 20:10
  • 1
    @Mr.T Do you mean "what is the meaning?" or "what is the purpose?". The meaning is quite clearly that the first operand is less than the second – Cave Johnson May 18 '16 at 20:17
  • What is the purpose? Sorry i picked a wrong vocabulary. :) – Mr.T May 18 '16 at 20:18
  • @Mr.T, in the answer I linked to: "But 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)**. " – HPierce May 18 '16 at 20:24

1 Answers1

2

You have three possibilities when comparing the two values that are passed to a comparison function: $a < $b, $a == $b, or $a > $b. So you need three distinct return values and PHP has chosen the integers: -1, 0, and 1. I guess it could just as easily be strings lesser, equal and greater or integers 5, 7 and 9 or any combination, but it's not.

From the manual usort()

The comparison function must 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.

  • $a < $b return -1
  • $a == $b return 0
  • $a > $b return 1

This is NOT how types work in PHP, but you can think of it like this: is $a > $b? where -1 means false, 1 means true and 0 means neither (equal).

AbraCadaver
  • 73,820
  • 7
  • 55
  • 81
  • I totally understand this. But return -1, return 0, return 1 are returning an integer of -1,0,1 or they are returning to boolean true/false stuff? That is all i wanna understand. – Mr.T May 18 '16 at 20:24
  • Thanks for your time. – Mr.T May 18 '16 at 20:44