What is the spaceship operator (<=>) in PHP?

October 26, 2018

The spaceship operator was a new feature in PHP 7. It looks like this: <==>

It will return 0 if both sides are equal. 1 if left is greater. -1 if right is greater.

<?php
    var_dump( [
        "1 <=> 1" => 1 <=> 1,
        "1 <=> 0" => 1 <=> 0,
        "0 <=> 1" => 0 <=> 1,
        "1 <=> \"1\"" => 1 <=> "1",
        "\"abc\" <=> \"abc\"" => "abc" <=> "abc",
        "\"zzz\" <=> \"abc\"" => "zzz" <=> "abc",
        ]);
array(6) {

["1 <=> 1"]=> 0

["1 <=> 0"]=> 1

["0 <=> 1"]=> -1

["1 <=> "1""]=> 0

[""abc" <=> "abc""]=> 0

[""zzz" <=> "abc""]=> 1

}