DEV Community

S A Nishan
S A Nishan

Posted on

help me to understand usort() in php

function so($a, $b) {
if($a == $b) {
return 0;
} else {
return $a > $b ? 1 : -1;
}
}

$num = [5,3,2,1,4];
usort($num, 'so');
foreach($num as $value) {
echo $value." ";
}

I write this code and the result is 1,2,3,4,5 and it is good! But I don't understand how this usort function actually work. I even don't understand what value $a and $b get to return 0, 1 or -1 can anyone explain me it?

Top comments (0)