This article mainly introduces php's method of auto-complementing 0 with insufficient digits Based on str_pad, which is very useful for generating fixed-digit numbers. For more information, see
This article mainly introduces php's method of auto-complementing 0 with insufficient digits Based on str_pad, which is very useful for generating fixed-digit numbers. For more information, see
The str_pad function can automatically fill in the blank digits in php to help us implement it. The str_pad () function fills the string with the specified length.
The str_pad () function fills the string with the specified length.
Syntax
Str_pad (string, length, pad_string, pad_type)
Parameter description
String is required. Specifies the string to be filled.
Length is required. Specify the length of the new string. If the value is smaller than the length of the original string, no operation is performed.
Pad_string is optional. The specified string for filling. The default value is blank.
Pad_type
Optional. Specify the other side of the string to be filled.
Possible values:
Example:
The Code is as follows:
$ CardCount = 10;
$ Arr = array ();
For ($ I = 1; $ I <= $ cardCount; $ I ++ ){
$ StrCard = str_pad ($ I, 10, '0', STR_PAD_LEFT );
$ Arr [] = $ strCard;
}
Print_r ($ arr );
The output result is as follows:
The Code is as follows:
Array ([0] => 0000000001 [1] => 0000000002 [2] => 0000000003 [3] => 0000000004 [4] => 0000000005 [5] => 0000000006 [6] => 0000000007 [7] => 0000000008 [8] => 0000000009 [9] => 0000000010)
I hope this article will help you with PHP programming.