Home  >  Article  >  Backend Development  >  How to add 0 to missing numbers in php

How to add 0 to missing numbers in php

藏色散人
藏色散人Original
2020-08-27 10:58:533826browse

php method to implement missing numbers: first create a PHP sample file; then use the PHP function "str_pad()" function to fill the string with a new length. The statement is "str_pad(string,length, pad_string,pad_type)"; finally output the result.

How to add 0 to missing numbers in php

Recommendation: "PHP Video Tutorial"

PHP generates 4 digits, add 0 in front if any is missing

Just a solution

You can use the PHP built-in function str_pad() function to fill the string to the new length.

str_pad(string,length,pad_string,pad_type)
//参数    描述
string      //必需。规定要填充的字符串。
length      //必需。规定新的字符串长度。如果该值小于字符串的原始长度,则不进行任何操作。
pad_string  //可选。规定供填充使用的字符串。默认是空白。
pad_type    //可选。规定填充字符串的哪边。
            //可能的值:
            STR_PAD_BOTH - //填充字符串的两侧。如果不是偶数,则右侧获得额外的填充。
            STR_PAD_LEFT - //填充字符串的左侧。
            STR_PAD_RIGHT - //填充字符串的右侧。默认。

Lift:

$num=128;
$num=str_pad($num,4,"0",STR_PAD_LEFT);   
echo $num;

Output:

0128

The above is the detailed content of How to add 0 to missing numbers in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn