PHP printf() function
Example
Output the formatted string:
<?php $number = 9; $str = "北京"; printf("In %s there are 쥕lion bicycles.", $str, $number); ?>
Definition and usage
The printf() function outputs a formatted string.
arg1,arg2,arg++ Parameters will be inserted into the main string at the percentage (%) symbol. The function is executed step by step. At the first % symbol, insert arg1, insert arg2, and so on.
Comment:If there are more % symbols than arg If you have parameters, you must use placeholders. Placeholders are inserted after the % symbol, consisting of a number and "\$". See example 2.
Tip:Related functions: sprintf(), vprintf(), vsprintf(), fprintf(), and vfprintf()
Syntax
printf(format,arg1,arg2,arg++)
Parameter | Description |
---|---|
format |
Required. Specifies the string and how to format the variables within it. Possible format values:
Additional format values. They must be placed between % and a letter (e.g., %.2f):
Comment:If multiple of the above format specifiers are used, they must be used in the order specified above and cannot be mixed. |
arg1 | Required. Specifies the argument to be inserted format The parameter at the first % symbol in the string. |
arg2 | Required. Specifies the argument to be inserted format The parameter at the second % symbol in the string. |
arg++ | Optional. Specifies the argument to be inserted format The parameters at the third, fourth, and so on % symbols in the string. |
Technical details
Return value: | Returns the length of the output string. |
PHP version: | 4+ |
More examples
Example 1
Using format specifier %f:
<?php $number = 123; printf("%f",$number); ?>
Example 2
Using placeholders:
<?php $number = 123; printf("With two decimal places: %1$.2f<br>No decimals: %1$u",$number); ?>
Example 3
Demonstration of all possible format values:
<?php $num1 = 123456789; $num2 = -123456789; $char = 50; // ASCII character 50 is 2 // Comment: The format value "%%" returns a percent sign printf("%%b = %b <br>",$num1); // Binary number printf("%%c = %c <br>",$char); // ASCII character printf("%%d = %d <br>",$num1); // Signed decimal number printf("%%d = %d <br>",$num2); // Signed decimal number printf("%%e = %e <br>",$num1); // Scientific notation (lowercase) printf("%%E = %E <br>",$num1); // Scientific notation (uppercase) printf("%鑾u <br>",$num1); // Unsigned decimal number (positive) printf("%鑾u <br>",$num2); // Unsigned decimal number (negative) printf("%%f = %f <br>",$num1); // Floating-point number (respecting local settings) printf("%%F = %F <br>",$num1); // Floating-point number (not respecting local settings) printf("%%g = %g <br>",$num1); // Shorter than %e and %f printf("%%G = %G <br>", $num1); // Shorter than %E and %f printf("%%o = %o <br>", $num1); // Octal Number printf("%%s = %s <br>", $num1); // String printf("%%x = %x <br>", $num1); // Lowercase Hexadecimal Number printf("%%X = %X <br>", $num1); // Uppercase Hexadecimal Number printf("%%+d = %+d <br>", $num1); // Positive Sign Specifier printf("%%+d = %+d <br>", $num2); // Negative Sign Specifier ?>
Example 4
Demonstration of String Specifiers:
<?php $str1 = "Hello"; $str2 = "Hello world!"; printf("[%s]<br>", $str1); printf("[%8s]<br>", $str1); printf("[%-8s]<br>", $str1); printf("[%08s]<br>", $str1); printf("[%'*8s]<br>", $str1); printf("[%8.8s]<br>", $str2); ?>