Andromeda Andromeda - 1 year ago 114
PHP Question

Formatting a number with leading zeros in PHP

I want have a variable which contains the value 1234567.

I want it to contain exactly 8 digits i.e. 01234567.

Is there a PHP function for that?

Answer Source

Use sprintf :

sprintf('%08d', 1234567);

Alternatively you can also use str_pad:

str_pad($value, 8, '0', STR_PAD_LEFT);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download