Home >Backend Development >PHP Tutorial >PHP pads zeros in front of numbers and pads fixed number of digits with 0s
When processing the order number, a fixed number of digits is required, such as a fixed four-digit format:
<code><span>1</span>-><span>0001</span><span>56</span>-><span>0056</span><span>288</span>-><span>0288</span><span>1992</span>-><span>1992</span></code>
You can use the PHP built-in function str_pad() function to fill the string to the new length.
<code>str_pad(<span>string</span>,<span>length</span>,pad_string,pad_type)<span> //参数 描述</span><span>string</span><span> //必需。规定要填充的字符串。</span><span>length</span><span> //必需。规定新的字符串长度。如果该值小于字符串的原始长度,则不进行任何操作。</span> pad_string <span> //可选。规定供填充使用的字符串。默认是空白。</span> pad_type <span> //可选。规定填充字符串的哪边。</span><span> //可能的值:</span> STR_PAD_BOTH -<span> //填充字符串的两侧。如果不是偶数,则右侧获得额外的填充。</span> STR_PAD_LEFT -<span> //填充字符串的左侧。</span> STR_PAD_RIGHT -<span> //填充字符串的右侧。默认。</span></code>
Example:
<code><span>$num</span>=<span>128</span>; <span>$num</span>=str_pad(<span>$num</span>,<span>4</span>,<span>"0"</span>,STR_PAD_LEFT); <span>echo</span><span>$num</span>;</code>
output
<code>0128</code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
The above introduces PHP's padding of zeros in front of numbers and padding of fixed digits with zeros, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.