Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use numbers starting with 0 in a variable in php

Tags:

php

Hi i need to save a 010 number in $number and if i do like this php will remove the starting 0

    $number = 010 

And echo of this will return 10 how can i make it not to remove the initial 0

BR Martin

like image 533
Martin Alderlöf Avatar asked Dec 08 '22 12:12

Martin Alderlöf


2 Answers

Use it as a String:

$number = '010';
like image 184
anubhava Avatar answered Dec 11 '22 10:12

anubhava


Use str_pad() function.

echo str_pad('10',3,'0',STR_PAD_LEFT)

http://php.net/manual/en/function.str-pad.php

like image 23
Dipesh Parmar Avatar answered Dec 11 '22 09:12

Dipesh Parmar