What is the difference between usort() and uksort() functions in PHP, and when should each be used?

The main difference between usort() and uksort() functions in PHP is the way they sort arrays. usort() sorts an array by values using a user-defined comparison function, while uksort() sorts an array by keys using a user-defined comparison function. usort() should be used when you want to sort an array based on its values, while uksort() should be used when you want to sort an array based on its keys.

// Example of using usort() to sort an array by values
$array = [3, 1, 4, 1, 5, 9, 2, 6, 5];
usort($array, function($a, $b) {
    return $a <=> $b;
});
print_r($array);

// Example of using uksort() to sort an array by keys
$array = ['b' => 2, 'a' => 1, 'c' => 3];
uksort($array, function($a, $b) {
    return $a <=> $b;
});
print_r($array);