I'm trying to do a usort in PHP, but I can't access global variables inside a usort function.
I've simplified my code down to bare bones to show what I mean:
$testglobal = 1;
function cmp($a, $b) {
global $testglobal;
echo 'hi' . $testglobal;
}
usort($topics, "cmp");
Assuming the usort runs twice, my expectations is this will be the output:
hi1hi1
Instead, my output is:
hihi
I've read the manual (http://us.php.net/usort) and I don't see any limitations on accessing global variables. If I assign the usort to a variable that I echo, it outputs 1, so the usort is definitely running successfully (plus, there are all those "hi's").
Am I doing something incredibly stupid here? If not, is there a workaround?