Introduction: In the daily work, often need to use the number 0 of the operation, such as: Date format Yyyy-mm-dd and so on.
There are a number of pre-and post-fill functions in PHP-today, we introduce two commonly used, the implementation of the number 0:
Please read the tutorial directly!
Fill function-str_pad
As the name implies, this function is for a string that can fill any other string with the specified string.
Syntax:str_pad(with filled string, fill the length, fill the string, fill the position)
Parameter resolution:
The length after filling-must be a positive integer
Fill position-There are three options:
Left: Str_pad_left
Right: Str_pad_right
Ends: Str_pad_both
Example 1:
Echo Str_pad (1,8, "0", str_pad_left);
Run result 1:
00000001
Example 2:
Echo Str_pad (1,8, "0", str_pad_right);
Run result 2:
10000000
Example 3:
Echo Str_pad (1,8, "0", Str_pad_both);
Run result 3:
00010000
Note: One of the notable details in the above example is that if the number of digits filled is an odd one, such as example three fills 7 0, the right priority. :)
Fill function-sprintf
1. Decimal supplement 0
+[after the decimal point 0]
Directly on the code:
Echo sprintf ("%01.3f", 1);
%01.3f means: a minimum of three digits after a decimal point is less than three 0, at least one before the decimal point, less than a zero of the floating-point number of the format behind the parameters
Run result 1:
1.000
Conclusion
On the complement of these several methods we can choose to use, in fact, each has the pros and cons,sprintf can ensure that you do not mistakenly operate it 1 1000000 haha,str_pad can guarantee you want to fill what.
Not finished, waiting to be updated ...
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4817667.html
[PHP Learning Tutorial]007. Number 0 (Num padding)