Why is this happening
In PHP (and most programming languages), numbers preceding 0 are treated as octal numbers . This is the base system number 8 and has numbers from 1 to 7.
Octal 0759 is equivalent to octal 075 (9 is discarded because there is no 9 in octal). Octal 075 is equivalent to decimal 61. PHP actually saves the number as octal, but when you exit with print / echo it is always in decimal, so 075 becomes 61.
Conversion
See Wikipedia on Eighth to Decimal Conversion. But this should give you a basic idea:
(075) 8 = (0 x 8 ^ 2) + (7 x 8 ^ 1) + (5 x 8 ^ 0)
(075) 8 = 0 + 56 + 5
(075) 8 = 61
Basically:
7 * 8 = 56 5 * 1 = 5
How to solve this problem
Just save the numbers as integers / strings and format them in the output.
Using sprintf() :
echo sprintf('%04d', $number);
Using str_pad() :
echo str_pad($number, 4, '0', STR_PAD_LEFT);
If you really want to keep the leading zero, save it as a string:
$number = '0759';
Amal murali
source share