How to add 0 to missing numbers in php

藏色散人
Release: 2023-03-05 10:24:01
Original
3828 people have browsed it

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 - //填充字符串的右侧。默认。
Copy after login

Lift:

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

Output:

0128
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template