What are the best practices for writing a comparison function in PHP to be used with usort for sorting arrays?

When writing a comparison function in PHP to be used with usort for sorting arrays, it is important to follow these best practices: 1. The comparison function should return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. 2. The comparison function should be a standalone function or a method of a class that implements the Comparable interface. 3. The comparison function should be deterministic and not rely on any external state.

// Example of a comparison function for sorting an array of numbers in ascending order
function compareNumbers($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

// Usage with usort
$array = [3, 1, 2, 5, 4];
usort($array, 'compareNumbers');
print_r($array);