PHP vfprintf()
Function
Example
Write some text to the text file named "test.txt":
The above code will output:
The following text will be written to the file "test.txt":
Definition and Usage
The vfprintf()
function writes a formatted string to a specified output stream (such as a file or database).
Unlike fprintf()
, the arguments in vfprintf()
are located in an array. The array elements will be inserted at the percent (%) symbols in the main string. The 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.
Note: If there are more % symbols than arguments, you must use placeholders. Placeholders are inserted after the % symbol and consist of a number and a "$". See Example 2.
Tip: Related functions: fprintf(), printf(), sprintf(), vprintf(), and vsprintf()
Syntax
Parameter | Description |
---|---|
stream | Required. Specifies where to write/output the string. |
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): + (prefixes numbers with + or - to indicate positive or negative. By default, only negative numbers are marked, positive numbers are not marked)<br> ' (specifies what to use as padding, default is a space. It must be used with a width specifier. For example: %'x20s (uses "x" as padding))<br> - (left adjust the value)<br> [0-9] (specifies the minimum width of the value)<br> .[0-9] (specifies the number of decimal places or the maximum string length) Note: If multiple format values are used, they must be used in the order shown above, not in a scrambled 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 written string. |
---|---|
PHP Version: | 5+ |
--- | --- |
More Examples
Example 1
Write some text to a file:
The following text will be written to the file "test.txt":
Example 2
Using placeholders:
The following text will be written to the file "test.txt":
Example 3
Using printf()
to demonstrate all possible format values: