CKTIS / PHP
Three-way comparison (<=>)
Return the comparison as an integer in a sorting callback. Swapping operands reverses the sort order.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
$left <=> $rightReturn the comparison as an integer in a sorting callback. Swapping operands reverses the sort order.
For two numbers the sign of the result gives their ordering. A negative value means the first is smaller, zero means equal, and a positive value means the first is larger. This is the return convention for an usort comparator.
5 <=> 8→-18 <=> 8→011 <=> 8→1$items = [3,9,2]; usort($items, fn($a, $b) => $b <=> $a); echo json_encode($items);→[9,3,2]
Examples
<?php
echo json_encode(5 <=> 8), PHP_EOL;
echo json_encode(8 <=> 8), PHP_EOL;
echo json_encode(11 <=> 8), PHP_EOL;
$items = [3, 9, 2];
usort($items, fn($a, $b) => $b <=> $a);
echo json_encode($items), PHP_EOL;
Result
-1 0 1 [9,3,2]