Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Function called with leading zero parameter gets zero inside

Tags:

function

php

zero

I created a php (5.2.6) function simple as is:

file a.php:

function la ($n) {
    echo "$n";
}

file b.php:

include ('a.php');
// got many calls of function with leading zero in parameter
la (01);
la (02);
...
la (07);
la (08);
la (09);
la (10);

Sometimes it prints 1, 2... 9, 10, sometimes it prints 0 instead 8 or 9! Looks random. Why? Why only 8 or 9? Indeed, I can avoid this zero. But, since I found strange behaviour, I want to ask you: what is the secret of this weird behaviour? Or I am weird. Thank you.

like image 360
michaelloop Avatar asked May 02 '26 09:05

michaelloop


1 Answers

Leading 0 conventionally indicates a number written in octal notation instead of decimal; 8 and 9 are not valid octal digits. (I admit to not knowing why PHP would care; it's not a conventional notation for web stuff.)

like image 189
geekosaur Avatar answered May 04 '26 22:05

geekosaur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!