PHP Question: Octal Values
Published by philipnorton42 on Fri, 04/15/2011 - 11:50Question
If I assign and print a value like this:
1 2 | $a = 012; print $a; |
I get back a value of '10', why?
Answer
This is because prefixing a number with a 0 in PHP forces the number to be interpreted as an octal value. However, it is stored internally as an integer value so when it is printed we get the integer value back. To get back the original octal value you could do something like this:
1 | print base_convert($a, 10, 8); |
Or even this:
1 | print decoct($a); |
Or even this:
1 | print sprintf("%o", $a); |
Category:
Comments
...
Paweł (not verified) - Fri, 04/15/2011 - 19:03in this case if you add first zero, php will interpreted the variable as octal number and convert this value to decimal number (?) ;p
The title of the post gave
Pete (not verified) - Wed, 04/27/2011 - 11:05The title of the post gave this away, otherwise you might have got me with this one :)
Add new comment