$compare_function = function($left, $right) { $l = (float) $left; $r = (float) $right; if ($l < $r) { return -1; } if ($l > $r) { return 1; } return 0; };
$a = 3; $b = 5; $result = $compare_function($a, $b); // -1
$values = array(5, 2, 4, 1, 3); usort($values, $compare_function); // 1, 2, 3, 4, 5
$compare_function = function(float $left, float $right) { return $left <=> $right; // the "spaceship" operator };
<=>
) is available from PHP 7 and later and is often useful when sorting values.