Return Int Not Bool
<?php
$array = range(0, 10);
usort($array, withBool(...));
print(implode(', ', $array)).PHP_EOL;
// 9, 10, 7, 8, 5, 6, 4, 3, 2, 1, 0
usort($array, withInt(...));
print(implode(', ', $array)).PHP_EOL;
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
function withBool($a, $b) : bool { return $a <=> $b; }
function withInt($a, $b) : int { return $a <=> $b; }
?>
There is a cute deprecation warning, when using a closure to sort an array: Deprecated: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero.
And if this deprecation is ignored, the boolean is cast automatically to an integer. As PHP turns 0 to false, and everything else, including 1 and -1 to true, this leads to surprising sorting effect.
Just trust the deprecation notice.
See Also
PHP Error Messages
PHP Features
Last updated: 10 September 2026