Wednesday, January 2, 2019

Formatting a number with leading zeros in PHP



I have a variable which contains the value 1234567.




I would like it to contain exactly 8 digits, i.e. 01234567.



Is there a PHP function for that?


Answer



Use sprintf :



sprintf('%08d', 1234567);



Alternatively you can also use str_pad:



str_pad($value, 8, '0', STR_PAD_LEFT);

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...