In PHP, you can use the str_pad() function to return four digits, and add zeros if there are less than four digits. The syntax is "str_pad($num,4,"0",STR_PAD_RIGHT)"; the parameter "STR_PAD_RIGHT" is specified Pad zeros on the right side of a numeric string.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use str_pad () function to return four digits, if less than four digits are filled with zeros.
Example:
<?php echo str_pad(128,4,"0",STR_PAD_RIGHT)."<br>"; echo str_pad(12,4,"0",STR_PAD_RIGHT)."<br>"; echo str_pad(1283,4,"0",STR_PAD_RIGHT)."<br>"; ?>
Explanation:
str_pad() function can use specified characters to The string is padded to the new length.
Syntax:
str_pad(string,length,pad_string,pad_type)
Parameters | Description |
---|---|
string | Required. Specifies the string to be filled. |
length | Required. Specifies the length of the new string. If the value is less than the length of the original string, no operation is performed. |
pad_string | Optional. Specifies the string used for padding. The default is blank. |
pad_type | Optional. Specifies which side of the string to fill. Possible values:
|
Example:
<?php echo str_pad(128,4,"0",STR_PAD_RIGHT)."<br>"; echo str_pad(128,4,"0",STR_PAD_LEFT)."<br>"; echo str_pad(12,4,"0",STR_PAD_BOTH)."<br>"; ?>
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of How does php return four digits and add zeros if they are not enough?. For more information, please follow other related articles on the PHP Chinese website!