Bazinga777 Bazinga777 - 8 months ago 48
PHP Question

Why does $variable = 0759 give out 61?

I don't know how to google this one out so I an asking it here. Why does it happen that when I declare a variable

$something = 0759
that it turns into 61. I know the answer must be very simple so please forgive my sillyness.

pce pce
Answer Source

It is an Integer literal, you declare a octal number with a leading zero.

$something = 0759; // octal

The octal numeral system is a base-8 number system. You can only use Numbers between 0-7 (other numbers are discarded).

$a = 0759; 
$b = 075;
// bool(true)

You could skip the zeros with ltrim.

 $a = ltrim("0759", 0); 
 echo $a; // 759
 // and reformat as suggested with str_pad or printf
 echo str_pad($a, 4, "0", STR_PAD_LEFT);