Jaymin Noob Jaymin Noob - 1 year ago 68
PHP Question

how Leading Zero works in php?

Suppose I have a code which outputs, it as

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

I know that the leading zero indicates an octal number in PHP, but how does it interprets, how it can executed? Anyone can share it's execution step by step? What is the role of parser here? I have well researched 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