Funny PHP #1 – Octadecimal numbers explained

First let’s test this small piece of code

<?php
$a=022;
echo 'This is:'.$a;
?>

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.

Leave a Reply

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