PHP printf function
The PHP printf function can be used to output the formatted string. The operation process first inserts new characters into the original string and formats it, and then directly outputs the formatted new string on the web page. The usage is the same as sprintf The function is similar, but the difference is that the printf function can be formatted and output at one time. After the sprintf function is formatted, echo or print must be used to output the result. If the program does not need to be formatted after the string, For other processing, but to output to the web page immediately, the printf function is quite convenient.
Basic syntax of PHP printf function
string printf (string $format, $args1, $args2, $args3, $arg4, $arg5 ...)
The first parameter in the parentheses of the printf function, $format, is the original formatting conversion string, which may contain many formatting parts with percentages. You can set the format of the characters that will be brought in. About $ For format parameters, please refer to the parameter table on the sprintf function . Then there are many $args, which are the characters to be brought into the $format string. There can be many characters. Printf will bring in the $format string in order, complete the formatting, and finally format the new string. Output on the web page.
PHP printf function example 1
$string ='to';
$number = '5';
$format ='Welcome %s Wibibi.%f is float of %u.';
printf($format,$string,$number,$number) ;
?>
PHP printf function example 2: Control the number of decimal places of floating-point numbers
$number = '5';
$format = "%1\$.3f is float of %u.";
printf($format,$number,$number);
?>
Post a Comment
0 Comments