Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does $variable = 0759 give out 61? [duplicate]

Tags:

variables

php

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.

like image 706
Bazinga777 Avatar asked Feb 26 '14 12:02

Bazinga777


1 Answers

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;
var_dump($a==$b);
// bool(true)

http://www.php.net/manual/en/language.types.integer.php

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);
like image 81
pce Avatar answered Sep 23 '22 07:09

pce