PHP vprintf() function
Example
Output the formatted string:
<?php $number = 9; $str = "Beijing"; vprintf("There are 쥕lion bicycles in %s.", array($number, $str)); ?>
Definition and Usage
The vprintf() function outputs a formatted string.
Unlike printf(), the parameters in vprintf() are located in an array. Array elements will be inserted into the main string at the percentage (%) symbol. This function is executed step by step. At the first % symbol, the first array element is inserted, at the second % symbol, the second array element is inserted, and so on.
Comment:If the % symbol is more than arg If parameters are provided, you must use placeholders. Placeholders are inserted after the % symbol, consisting of numbers and '\$'. See example 2.
Tip:Related functions: sprintf(), printf(), vsprintf(), fprintf(), and vfprintf()
Syntax
vprintf(format,argarray)
Parameter | Description |
---|---|
format |
Required. Specifies the string and how to format the variables within it. Possible format values:
Additional format values. Must be placed between % and a letter (for example %.2f):
Comment:If multiple format values are used, they must be in the order listed above. |
argarray | Required. An array with parameters, which will be inserted into format The %% symbol in the string. |
Technical details
Return value: | Returns the length of the output string. |
PHP version: | 4.1.0+ |
More examples
Example 1
Using format value %%f:
<?php $num1 = 123; $num2 = 456; vprintf("%f%f",array($num1,$num2)); ?>
Example 2
Using placeholders:
<?php $number = 123; vprintf("With two decimal places:%1$.2f<br>No decimal:%1$u",array($number)); ?>
Example 3
Demonstrate all possible format values using printf():
<?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 the local settings) printf("%%F = %F <br>",$num1); // Floating-point number (not respecting the 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); // Hexadecimal Number (Lowercase) printf("%%X = %X <br>",$num1); // Hexadecimal Number (Uppercase) 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!"; vprintf("[%s]<br>",array($str1)); vprintf("[%8s]<br>",array($str1)); vprintf("[%-8s]<br>",array($str1)); vprintf("[%08s]<br>",array($str1)); vprintf("[%'*8s]<br>",array($str1)); vprintf("[%8.8s]<br>",array($str2)); ?>