Format numbers with leading zeros in PHP
P粉101708623
2023-08-23 16:58:48
<p>I have a variable that contains the value <code>1234567</code>. </p>
<p>I want it to contain exactly 8 digits, which is <code>01234567</code>. </p>
<p>Is there a PHP function? </p>
Assume the value is in $value:
Echo it:
printf(" d", $value);
Get it:
$formatted_value = sprintf(" d", $value);
This should do the trick
Use
sprintf
: p>Alternatively, you can use
str_pad
: