| aharvey | Yes | 2015-02-02 12:53 |
| ajf | Yes | 2015-02-02 12:06 |
| auroraeosrose | Yes | 2015-02-09 16:54 |
| brandon | Yes | 2015-02-02 14:42 |
| bwoebi | No | 2015-02-02 16:25 |
| chregu | Yes | 2015-02-02 12:31 |
| colder | Yes | 2015-02-16 11:19 |
| cyberline | Yes | 2015-02-06 20:18 |
| datibbaw | No | 2015-02-09 03:43 |
| davey | Yes | 2015-02-02 12:16 |
| derick | Yes | 2015-02-02 12:23 |
| dm | Yes | 2015-02-10 13:45 |
| duodraco | Yes | 2015-02-03 10:21 |
| fa | No | 2015-02-04 09:31 |
| francois | Yes | 2015-02-06 07:58 |
| guilhermeblanco | No | 2015-02-04 02:39 |
| gwynne | Yes | 2015-02-06 09:54 |
| hywan | Yes | 2015-02-02 12:20 |
| irker | Yes | 2015-02-03 11:36 |
| jedibc | Yes | 2015-02-03 08:25 |
| jgmdev | Yes | 2015-02-13 05:38 |
| jwage | Yes | 2015-02-16 15:19 |
| kalle | Yes | 2015-02-02 14:37 |
| kinncj | Yes | 2015-02-03 00:54 |
| klaussilveira | No | 2015-02-06 16:11 |
| laruence | Yes | 2015-02-02 12:13 |
| lcobucci | Yes | 2015-02-04 05:14 |
| leigh | No | 2015-02-08 13:40 |
| levim | No | 2015-02-02 16:38 |
| lstrojny | Yes | 2015-02-12 21:43 |
| malukenho | Yes | 2015-02-04 06:24 |
| marco | Yes | 2015-02-13 00:43 |
| mbeccati | Yes | 2015-02-05 16:54 |
| mfischer | Yes | 2015-02-02 14:26 |
| mfonda | No | 2015-02-04 21:01 |
| mike | Yes | 2015-02-02 12:19 |
| mrook | Yes | 2015-02-02 12:23 |
| nikic | No | 2015-02-02 13:39 |
| pajoye | Yes | 2015-02-02 12:16 |
| pauloelr | Yes | 2015-02-14 20:01 |
| ramsey | Yes | 2015-02-09 16:57 |
| rdlowrey | No | 2015-02-06 07:51 |
| rdohms | Yes | 2015-02-02 12:29 |
| salathe | Yes | 2015-02-10 20:13 |
| santiagolizardo | Yes | 2015-02-02 12:51 |
| seld | Yes | 2015-02-02 12:19 |
| sobak | No | 2015-02-06 08:03 |
| stas | Yes | 2015-02-02 18:39 |
| stelianm | Yes | 2015-02-02 13:45 |
| thijs | Yes | 2015-02-02 12:18 |
| treffynnon | Yes | 2015-02-09 12:31 |
| weierophinney | Yes | 2015-02-02 13:22 |
| yohgaki | Yes | 2015-02-11 02:17 |
| yunosh | Yes | 2015-02-02 15:07 |
Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC
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:
if (($handle = fopen("people.csv", "r")) !== FALSE) {
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = $row;
}
fclose($handle);
}
// Sort by last name:
usort($data, function ($left, $right) {
return $left[1] <=> $right[1];
});
It makes writing ordering callbacks for use with usort() easier. Commonly, users write poor or incorrect ordering functions like this one:
function order_func($a, $b) {
return $a >= $b;
}
When users do write correct ordering functions, they have to be quite verbose:
function order_func($a, $b) {
return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
}
This becomes particularly bad when sorting by multiple columns lexicographically.
With this operator, you can easily write proper ordering functions, like this one:
function order_func($a, $b) {
return $a <=> $b;
}
Sorting by multiple columns is simpler now, too:
function order_func($a, $b) {
return [$a->x, $a->y, $a->foo] <=> [$b->x, $b->y, $b->foo];
}
Or:
function order_func($a, $b) {
return ($a->$x <=> $b->x) ?: ($a->y <=> $b->y) ?: ($a->foo <=> $b->foo);
}
It is also useful in some other contexts.
This introduces no backwards incompatible changes.
The next major version of PHP, currently PHP 7.0.
A T_SPACESHIP constant for use with ext/tokenizer has been added.
All existing comparison operators are unaffected by this addition.
None.
Voting started on 2015-02-02 and will to end on 2015-02-16. As this adds to the PHP language (and hence affects the PHP language specification) a 2/3 majority is required for acceptance. It is a Yes/No vote to accepting the RFC and merging the patch.
Vote: Accept the Combined Comparison (Spaceship) Operator RFC and merge patch into master? — Yes: 43 · No: 11 (closed)
A patch targeting php-src master is here: https://github.com/php/php-src/pull/1007
There is not yet a language specification patch.
Shafik's original patch against 5.6 was here: https://github.com/dshafik/php-src/compare/add-spaceship-operator