Home >Backend Development >PHP Tutorial >Use the str_pad function in PHP to generate product numbers in the form of numerical increments_PHP tutorial
Solution: $str="QB".str_pad(($maxid[0]["max(id)"]+1),5,"0",STR_PAD_LEFT );
where $maxid[0 ]["max(id)"]+1) is to use the max function to find the value with the largest ID from the database. The ID is the primary key and will not be repeated.
str_pad() function pads a string to the specified length.
Grammar
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 the side of the padding string. Possible values:
|