English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP String Manual of String Functions
The printf() function is used to output formatted strings.
int printf ( string $format[, mixed $args[, mixed $... ]])
Returns the formatted output string
It returns the length of the output string.
Serial number | Parameters and descriptions |
---|---|
1 | format Specify the string and how to format the variables within it.Possible format values:
additional format values. Must be placed between % and the letter (for example %.2f):
Note:If multiple of the above format values are used, they must be used in the order specified above and cannot be disrupted. |
2 | arg1 Required. Specify the parameter to be inserted into the first % symbol in the format string. |
3 | arg2 Optional. Specify the parameter to be inserted into the second % symbol in the format string. |
4 | arg ... Optional. Specify the parameter to be inserted into the format string at the third, fourth, and so on % symbols. |
Try the following example, output formatted data and string:
<?php ////Output formatted string printf("www.oldtoolbag.com simply easy learning\n"); //Use the format value %f to format the number: $number = 2123; printf("%f", $number); $str = "0758 jian"; $strA = "A"; $strB = "B"; $num1 = 5; $num2 = 5; $num3 = 0.25; $num4 = 3.2567; $num5 = 8; $num6 = 1.735; $num7 = 16777215; $num8 = 16777215; echo '<br />'; printf("%s %s", $strA, $strB); echo '<br />'; printf("Padding: %'%"10s //Specify the padding character as %string width as10 echo '<br />'; printf("Binary: %b", $num)1); echo '<br />'; printf("ASCII code: %c", $num)2); echo '<br />'; printf("Integer: %d", $num)3); echo '<br />'; 2f4); echo '<br />'; printf("Octal: %o", $num)5); echo '<br />'; printf("String: %s", $str); echo '<br />'; printf("Non-decimal: 眻, $num)6); echo '<br />'; printf("Hexadecimal: %x", $num)7); echo '<br />'; printf("Hexadecimal: %X", $num)8); ?>Test and see‹/›
Output result
www.oldtoolbag.com simply easy learning 2123.000000A2 B1 Padding: %0758 jian Binary: 101 ASCII code: Integer: 0 Floating-point: 3.26 Octal: 10 String: 0758 jian Non-decimal: 1 Hexadecimal: ffffff Hexadecimal: FFFFFF