Jaymin Noob Jaymin Noob -3 years ago 107
PHP Question

How is Leading Zero working in php?

Let's suppose I have a code which outputs


$i = 016;
echo $i / 2;
//ans will be 7

I know that the leading zero indicates an octal number in PHP, but how is it interpreted, how can it be executed? Can somebody share its execution step by step? What is the role of parser here? I have researched a lot and read all the previous answers but none are having any deep explanation.

Answer Source

When you preceed integers with zero in PHP, in that instance, 029.

It becomes octal.

So when you echo that, it will convert to its decimal form.

Which results to:

echo 016; //14 (decimal) valid octal

echo 029; // 2 (decimal) -  Invalid octal

Actually, its here stated in the manual

Valid octal:

octal       : 0[0-7]+

Note: Prior to PHP 7, if an invalid digit was given in an octal integer (i.e. 8 or 9), the rest of the number was ignored. Since PHP 7, a parse error is emitted.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download