PHP 8.5.0 Alpha 4 available for testing

Voting

: three plus four?
(Example: nine)

The Note You're Voting On

"Matthew Rice"
12 years ago
While this may seem obvious, user-defined array sorting functions ( uksort(), uasort(), usort() ) will *not* be called if the array does not have *at least two values in it*.

The following code:

<?php

function usortTest($a, $b) {
var_dump($a);
var_dump($b);
return -
1;
}

$test = array('val1');
usort($test, "usortTest");

$test2 = array('val2', 'val3');
usort($test2, "usortTest");

?>

Will output:

string(4) "val3"
string(4) "val2"

The first array doesn't get sent to the function.

Please, under no circumstance, place any logic that modifies values, or applies non-sorting business logic in these functions as they will not always be executed.

<< Back to user notes page

To Top