PHP vprintf()
Function
Example
Output a formatted string:
Definition and Usage
The vprintf()
function outputs a formatted string.
Unlike printf()
, the arguments in vprintf()
are contained within an array. The array elements will be inserted at the percent (%) symbols in the main string. The function executes 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.
Note: If there are more % symbols than arguments, you must use placeholders. Placeholders are inserted after the % symbol and consist of a number and "$". See Example 2.
Tip: Related functions: sprintf(), printf(), vsprintf(), fprintf(), and vfprintf()
Syntax
Parameter | Description |
---|---|
format | Required. Specifies the string and how to format the variables in it. Possible format values: %% - Returns a percent sign %<br> %b - Binary number<br> %c - Character corresponding to the ASCII value<br> %d - Signed decimal number (negative, zero, positive)<br> %e - Lowercase scientific notation (e.g., 1.2e+2)<br> %E - Uppercase scientific notation (e.g., 1.2E+2)<br> %u - Unsigned decimal number (greater than or equal to 0)<br> %f - Floating point number (locale aware)<br> %F - Floating point number (non-locale aware)<br> %g - Shorter of %e and %f<br> %G - Shorter of %E and %f<br> %o - Octal number<br> %s - String<br> %x - Hexadecimal number (lowercase letters)<br> %X - Hexadecimal number (uppercase letters) Additional format values. Must be placed between % and the letter (e.g., %.2f): + (Adds a + or - sign before the number. By default, only negative numbers are marked, positive numbers are not marked)<br> ' (Specifies what to use as padding. Default is space. It must be used with a width specifier. For example: %'x20s (uses "x" as padding))<br> - (Left-justifies the variable value)<br> [0-9] (Specifies the minimum width of the variable value)<br> .[0-9] (Specifies the number of decimal places or the maximum string length) Note: If using multiple format values, they must be used in the order shown above, not in a mixed order. |
argarray | Required. An array with arguments to be inserted at the % symbols in the format string. |
Technical Details
Return Value: | Returns the length of the outputted string. |
---|---|
PHP Version: | 4.1.0+ |
--- | --- |
More Examples
Example 1
Using the format value %f:
Example 2
Using placeholders:
Example 3
Using printf()
to demonstrate all possible format values:
Example 4
Demonstration of string specifiers: