PHP vfprintf() Function

PHP vfprintf() Function

The vfprintf() function in PHP writes a formatted string to a specified output stream.

Syntax

vfprintf ( resource $handle , string $format , array $args ) 

Parameter

handle(required)- This parameter represents where to write the string.

format(required)- This parameter specifies the string and how to format the variables in it.

The conversion specification of this parameter follows the given prototype: %[flags][width][.precision]specifier.

Flags

Flag Description
- Left-justify within the given field width and by default, it follows the right justification
+ It signifies the prefix positive numbers with a plus sign (+). By default, only negative is prefixed with a negative sign.
(space) It pads the result with spaces. This is the default.
0 It only left-pads numbers with zeros. With “s” specifiers this can also right-pad with zeros.
(char) It pads the result with the character (char).

Width

It signifies an integer that states how many characters (minimum) this conversion should result in.

Precision

  • e, E, f and F specifiers: The number of digits to be printed after the decimal point (by default, it is 6).
  • g and G specifiers: The maximum number of significant digits to be printed.
  • s specifier: It acts as a cutoff point, setting a maximum character limit to the string.

If the period is specified without an explicit value for precision, 0 is assumed.

Specifiers

Specifier Description
% It represents a literal percent character, and no other argument is required.
B In this, the argument is treated as an integer and presented as a binary number.
C In this, the argument is treated as an integer and presented as the character with that ASCII.
D In this, the argument is treated as an integer and presented as a (signed) decimal number.
E In this, the argument is treated as scientific notation. The precision specifier stands for the number of digits after the decimal point.
E It is like the “e” specifier, but it uses the uppercase letter.
F The argument is treated as a float and presented as a floating-point number (locale aware).
F The argument is treated as a float and presented as a floating-point number (non-locale aware). 
G It represents a general format.
G Unlike the “g” specifier but it also uses E and F.
O In this, the argument is treated as an integer and presented as an octal number.
S In this, the argument is treated and presented as a string.
U In this, the argument is treated as an integer and presented as an unsigned decimal number.
X The argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).
X The argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).

Type Handling

Type Specifiers
String S
Integer d, u, c, o, x, X, b
Double g, G, e, E, f, F

args(optional)- This parameter represents an array with arguments inserted at the third, fourth, etc. %-sign in the format string.

Return

This function returns the length of the outputted string

Example 1

 

Output: The given text will be written at file text.txt.

432.000000321.000000

Example 2

 

Output

The number with 2 decimals: 213.00
The number with no decimals: 213