Could anyone explain why $property is 'name' outside of the function and null inside? I’ve checked with var_dump, and I can’t figure out why this isn’t working. Isn’t the global keyword there supposed to bring it inside?
$property = $_GET['property']; // 'name'
function cmp($a, $b)
{
global $property;
return strnatcmp($a->$property, $b->$property);
}
usort($files, 'cmp');
Alternatively, do you have a better way of doing this?
Check out this question: Errors when using array_push — "First argument should be an array"
If the provided piece of code is called within another function (or a file included through another function), then $property isn’t global, and thus cannot be retrieved through
You could always do something simpler, since $_GET is already global.