Home > Article > Backend Development > How to add 0 if there are not enough digits in php?
In PHP, you can use the sprintf() function to supplement the number with insufficient digits with 0. This function is used to write the formatted string into a variable. When the parameter is set to "d ", you can add zeros to 5 digits, the syntax is "sprintf(" d", value)".
The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer
sprintf() function writes the formatted string into a variable.
arg1, arg2, parameters will be inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, insert arg1, at the second % sign, arg2, and so on.
The syntax is:
sprintf(format,arg1,arg2,arg++)
The example is as follows:
<?php $s1=sprintf("%05d",1234); echo $s1 ,"<br>"; ?>
Output result:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to add 0 if there are not enough digits in php?. For more information, please follow other related articles on the PHP Chinese website!