Originally posted by thorpe
i tried your function a few times with a few different numbers passed to it. sometimes im getting some odd results though.
returns;
054
because i dont completely understand your function i have no idea how to fix this.
That's because the function returns an associative array; if you were to look at the keys as well, you'd see they were paired up properly. What happened is that $result['exec'] was initialised first (to zero), then $result['write'] was initialised, then $result['read']. If the first line read
$result['write'] = $result['read'] = $result['exec'] = 0;
then just printing out values via foreach() would output them in the same order. But still, as long as the keys are paired with the right values it shouldn't matter.
But it doesn't matter anyway, since those values are never even used.
I'd get rid of the loop and extra variables.
function permissions($perm) {
$perm = str_pad($perm, 3, '0', STR_PAD_LEFT);
return array(
'exec' => min(5,max(0,$perm[0])),
'read' => min(5,max(0,$perm[1])),
'write' => min(5,max(0,$perm[2])),
);
}
Getting them from a database? Yup, should be fine; just as long as you don't do something literal like permissions(055) in your code. As mrhappiness said, that's equivalent to permissions(45).