How to format a number with leading zeros in PHP?

Spread the love

Sometimes, we want to format a number with leading zeros in PHP.

In this article, we’ll look at how to format a number with leading zeros in PHP.

How to format a number with leading zeros in PHP?

To format a number with leading zeros in PHP, we can use the str_pad function.

For instance, we write

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

to pad the $value number with leading 0’s until the number has 8 digits.

A new string with the leading zeroes is returned.

Conclusion

To format a number with leading zeros in PHP, we can use the str_pad function.

Leave a Reply

Your email address will not be published.