There are at least two functions in PHP that can help us quickly achieve digital 0:
The first is the PHP str_pad function:
Copy Code code as follows:
#str_pad-Fills a string with another string for a specified length
As the name suggests, this function is for strings, filling any other string with the specified string
Str_pad parameter Description:
Copy Code code as follows:
String Str_pad (string $input, int $pad _length [, string $pad _string = "" [, int $pad _type = str_pad_right]])
#常用参数说明: Str_pad (with filled string, filling length, filling string, filling position)
The length must be a positive integer, filling position has three options,
Left: Str_pad_left
Right: Str_pad_right
Both ends: Str_pad_both
Example Show:
Copy Code code as follows:
Echo Str_pad (1,8, "0", str_pad_left);
#结果: 00000001
Echo Str_pad (1,8, "0", str_pad_right);
#结果: 10000000
Echo Str_pad (1,8, "0", Str_pad_both);
#结果: 00010000
One notable detail in the above example is that if the number of digits that are filled is odd, for example three to fill 7 0, the right is preferred.
Here's another way to make a zero:
PHP sprintf function:
Copy Code code as follows:
#sprintf-Returns a formatted string
This function is more flexible, waiting for scholars to dig deep, here mainly to achieve the value of the left 0 (or 0 after the decimal point) treatment;
Let's see the left 0.
Copy Code code as follows:
Echo sprintf ("%05d", 1);
#%05d Meaning: Use a 5-digit number to format the following parameters, if less than 5 digits to fill 0
# run result is 00001
Look at the decimal point and make up 0.
Copy Code code as follows:
Echo sprintf ("%01.3f", 1);
#%01.3f meaning: With a decimal point after at least three bit less than three digits to fill 0, the decimal point before the least one, less than one zero zeros of floating-point numbers format the following parameters
# Run Result: 1.000
In addition, you can write a custom function to handle it.
The writing code each has the superiority also to have the inferior, everybody may choose suitable;
Note: sprintf can ensure that not to mistake the operation of 1 to make up 1000000,str_pad can guarantee what to fill.