Pass octal value to function

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Does anyone know how to pass an octal value to a function
I am trying to create a class to handle directory permissions but
passing the permission settings is going nowhere.

Octals seem to automatically get converted to decimal when sent and
when trying to convert back using decoct the 0 is dropped.
I have also tried sending as a string but chmod() does not accept
string input and I haven't been able to convert back to a number &
retain the leading 0.



function test($myvar){
echo "PASSED VAL:".$myvar;
echo "DECOCT:".decoct($myvar);
// DECOCT:777
echo "STRING:".(('0'.decoct($myvar))*1);
// STRING:777

Re: Pass octal value to function

Quoted text here. Click to load it

ok so what's wrong there? i guess if you'd pass the (decimal) 511 to chmod,
it'll interpret it just like it'd interpret a literal 0777.

Quoted text here. Click to load it

echo "STRING:". '0'.(decoct($myvar)*1);

you first prepended the '0', making it a string, and then multiplied by 1,
turning it into an integer again.

Re: Pass octal value to function

You could just send the octal as a string -> "0777" instead of just
0777. PHP is pretty loose, strings and integers can be interchanged.

Site Timeline