This RFC adds 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 <, <=, ==, >=, > .

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. It uses exactly the same comparison rules as used by our existing comparison operators: <, <=, ==, >= and > . (See the manual for details)

This "three-way comparison operator", also known as the “spaceship operator” (a common name in other languages), works on all standard PHP values. It exists in other languages: Perl, Ruby, and Groovy.

For consistency with Perl, it has the same precedence as == and != .

It is implemented by using the result of the existing internal compare_function that underlies the other comparison operators. The existing comparison operators could be considered mere shorthands for <=> :

operator <=> equivalent $a < $b ($a <=> $b) === -1 $a <= $b ($a <=> $b) === -1 || ($a <=> $b) === 0 $a == $b ($a <=> $b) === 0 $a != $b ($a <=> $b) !== 0 $a >= $b ($a <=> $b) === 1 || ($a <=> $b) === 0 $a > $b ($a <=> $b) === 1

Here are some examples of its behaviour:

// Integers echo 1 <=> 1 ; // 0 echo 1 <=> 2 ; // -1 echo 2 <=> 1 ; // 1 // Floats echo 1.5 <=> 1.5 ; // 0 echo 1.5 <=> 2.5 ; // -1 echo 2.5 <=> 1.5 ; // 1 // Strings echo "a" <=> "a" ; // 0 echo "a" <=> "b" ; // -1 echo "b" <=> "a" ; // 1 echo "a" <=> "aa" ; // -1 echo "zz" <=> "aa" ; // 1 // Arrays echo [ ] <=> [ ] ; // 0 echo [ 1 , 2 , 3 ] <=> [ 1 , 2 , 3 ] ; // 0 echo [ 1 , 2 , 3 ] <=> [ ] ; // 1 echo [ 1 , 2 , 3 ] <=> [ 1 , 2 , 1 ] ; // 1 echo [ 1 , 2 , 3 ] <=> [ 1 , 2 , 4 ] ; // -1 // Objects $a = ( object ) [ "a" => "b" ] ; $b = ( object ) [ "a" => "b" ] ; echo $a <=> $b ; // 0 $a = ( object ) [ "a" => "b" ] ; $b = ( object ) [ "a" => "c" ] ; echo $a <=> $b ; // -1 $a = ( object ) [ "a" => "c" ] ; $b = ( object ) [ "a" => "b" ] ; echo $a <=> $b ; // 1 $a = ( object ) [ "a" => "b" ] ; $b = ( object ) [ "b" => "b" ] ; echo $a <=> $b ; // 0

Usort Example: