Copy CodeThe code is as follows:
sprintf () function, return value to formatted string
String sprintf (String $format [, Mixed $args [, mixed $ ...])
$y = 11;
$m = 3;
$d = 9;
$date = Sprinf ('%04d-%02d-%02d ', $y, $m, $d);
Echo $date; 0011-0
printf () function, return value is formatted string length
int printf (string $format [, Mixed $args [, mixed $ ...])
$num = 3.14;
printf ("Character padding% ' #6.2s", $num); # #3.14
The character length is 6, there are several points after 2, less than 6 bits, #填充
sprintf () differs from printf ()
Syntax format, just the return value is different
Definition and usage
The sprintf () function writes a formatted string to a variable.
Grammar
sprintf (format,arg1,arg2,arg++)
Parameters |
Description |
Format |
Necessary. Conversion format. |
Arg1 |
Necessary. Specifies the parameter that is inserted into the first% symbol in the format string. |
Arg2 |
Optional. Specifies the parameter that is inserted into the second% symbol in the format string. |
arg++ |
Optional. Specifies the parameter that is inserted into the format string third to fourth, and so on, in the% symbol. |
Description
The parameters format is the converted format, starting with the percent sign ("%") to the end of the converted character. The following possible format values are:
- Percent-return percentage symbol
- %b-Binary number
- %c-Characters in accordance with ASCII values
- %d-Signed decimal number
- %e-Sustainable counting method (e.g. 1.5e+3)
- %u-Unsigned decimal number
- %f-Floating point (Local settings Aware)
- %F-Floating point number (not local settings aware)
- %o-Eight binary number
- %s-String
- %x-16 binary number (lowercase letters)
- %x-16 decimal digits (uppercase letters)
Arg1, arg2, + + etc parameters will be inserted into the main string percent percent (%) Symbol. This function is executed step-by. In the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on.
Example
Example 1
Copy the Code code as follows:
$str = "Hello";
$number = 123;
$txt = sprintf ("%s World". Day number%u ", $STR, $number);
Echo $txt;
?>
Output:
Hello World. Day Number 123
Example 2
Copy the Code code as follows:
$number = 123;
$txt = sprintf ("%f", $number);
Echo $txt;
?>
Output:
123.000000
Example 3
Copy the Code code as follows:
$number = 123;
$txt = sprintf ("With 2 decimals:%1\$.2f
With no decimals:%1\ $u ", $number);
Echo $txt;
?>
Output:
With 2 decimals:123.00
With no decimals:123
For more details, refer to Http://www.jb51.net/w3school/php/func_string_sprintf.htm
The above describes the PHP sprintf function use case parsing, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.