Funny PHP #1 – Octadecimal numbers explained

First let’s test this small piece of code

<br />
&lt;?php<br />
$a=022;<br />
echo 'This is:'.$a;<br />
?&gt;<br />

What will this code display? “This is 022” or “This is 22”? None.
This will display “This is 18”. Why 18? Because numbers beginning with 0 are considered as octadecimal numbers.
Let’s convert 022 to decimal numbers.
As 22todecimal=2*8^1+2*8^0=16+2=18.
That’s why the codepiece displays 18.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: