Format numbers with leading zeros in PHP
P粉101708623
P粉101708623 2023-08-23 16:58:48
0
2
436
<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>
P粉101708623
P粉101708623

reply all(2)
P粉056618053

Assume the value is in $value:

  • Echo it:

    printf(" d", $value);

  • Get it:

    $formatted_value = sprintf(" d", $value);

This should do the trick

P粉018653751

Usesprintf: p>

sprintf('%08d', 1234567);

Alternatively, you can use str_pad:

str_pad($value, 8, '0', STR_PAD_LEFT);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!