The spaceship operator <=> is a three-way comparison operator. It returns -1 when the left side is smaller, 0 when both sides are equal, and 1 when the left side is greater. PHP 7.0 introduced it.
echo 1 <=> 2; // -1
echo 2 <=> 2; // 0
echo 3 <=> 2; // 1
It follows PHP's usual comparison rules, so it works on numbers, strings and arrays. Its main use is sorting callbacks, because usort() expects exactly this kind of result. For example, usort($users, fn($a, $b) => $a->age <=> $b->age) sorts users by age.
It also compares several values at once. Comparing arrays, as in [$a->last, $a->first] <=> [$b->last, $b->first], sorts by last name and then by first name. Swap $a and $b to sort in descending order.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓