PHP String vfprintf() Function
The vfprintf() is a built-in function of PHP, which is used to convert/transform a formatted string to a specific output stream. In vfprintf(), the arguments are placed in an array. The elements of the array will be inserted along with percent (%) signs in the main string. The vfprintf() function works “step-by-step”. With each % sign, an element is inserted.
Note: If % signs are more than the arguments, then we have to use the placeholders. A placeholder is inserted after the % sign, and contains the arguments such as – number and “$”.
PHP 5 and above versions support this function. Unlike fprintf(), the arguments in vfprintf(), are placed in array. This function returns the length of the string written in the file.
Syntax
The syntax of the vfprintf() is given below, in which all three arguments are mandatory to pass in this function.
Parameters
$stream (required) – This is the mandatory parameter of vfprintf() function, which specifies that where to write the string.
$format (required) – It is also a mandatory parameter of vfprintf() function, which specifies the string, and how to format a variable in it.
Possible format values:
- %% – It returns a percent symbol
- %b – Represented as a binary number
- %c – Display characters according to the ASCII values.
- %d – Represented as a signed decimal number.
- %e – The arguments are treated as scientific notation using the lowercase letter (e.g. 3.2e+2)
- %E – Similar to e specifier but uses uppercase (e.g. 3.2E+2)
- %u – unsigned decimal numbers
- %f – represented as a floating-point number (locale aware)
- %F – Also represented as a floating-point number but non-locale aware
- %g – Shorter of %e and %f
- %G – Shorter of %E and %F
- %o – Represented as an octal number
- %s – Treated as well as represented as a string
- %x – Represented as a hexadecimal number with lowercase letters
- %X – Represented as a hexadecimal number but with uppercase letters
Additional format values:
These values are placed between the % sign and the letters, e.g., %.2f
- + = Prefix both + and – sign, by default only negative numbers are marked with -ive sign.
- – = Left-justify within the given variable value, Right justification is by default.
- ‘ = Pads the result with the character.
- (space) = Pads the result with spaces.
- 0 = Only left-pads numbers with zeros. With the s specifier, it can also right-pads with zeros.
$argarray (required) – An array with arguments is to be inserted at the % signs in the format string. It is a mandatory parameter of vfprintf() function.
Return values
The vfprintf() function returns the length of the written string.
Examples
Below some examples are given to understand the working of vfprintf().
Example 1
In the below example, we will open a test.txt file in write mode (“w”) to write some data in it. If test.txt file does not exist, it will automatically create the file in the same folder, and then we can write the text in it.
Output:
34
The following text will be written in the test.txt file.
There is 71 percent water on earth.
Note: In write mode “w”, the existing data of the file will be removed and override the new data.
Example 2
Output:
32
The following data will be written in the test.txt file.
512.000000 18446744073709551370
Example 3
Output:
45
The following data will be written in the test.txt file.
With 3 decimal: 278.000 Without decimal: 278