PHP fprintf() Function

PHP fprintf() Function

The fprintf() function in PHP outputs a formatted string.

This function operates as printf() but accepts an array of arguments, rather than a variable number of arguments.

Syntax

vprintf ( string $format , array $args )

Parameter

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

783.000000456.000000

Example 2

 

Output

2019-08-01

Example 3

 

Output

There are 9 towers owned by Reema.

Example 4

<?php
// initializing the number
$value = 9;  
//initializing the string 
$str = "TutorialandExample";  
$file = fopen("test.txt","w");  
echo fprintf($file," There are %u cycles in %s.",$value,$str);  
?> 

Output

42

Example 5

<?php
$number1 = 129784563;  
$number2 = -129784563;  
$char = 80; // The ASCII Character 80 is P
// Note: The format value "%%" returns a percent sign  
printf("%%b = %b ",$number1); // return the Binary number   
printf("\n%%c = %c ",$char); // The ASCII Character P 
printf("\n%%d = %d ",$number1); // Signed decimal number  
printf("\n%%d = %d ",$number2); // Signed negative decimal number  
printf("\n%%e = %e ",$number1); // Scientific notation (lowercase)  
printf("\n%%E = %E :",$number1); // Scientific notation (uppercase)  
?> 

Output

%b = 111101111000101101011110011 
%c = P 
%d = 129784563 
%d = -129784563 
%e = 1.297846e+8 
%E = 1.297846E+8  

Example 6

<?php
$number1 = 129784563;  
$number2 = -129784563;  
// Note: The format value "%%" returns a percent sign  
printf("%%u = %u ",$number1); // It returns the Unsigned decimal number (positive)  
printf("\n%%u = %u ",$number2); // It returns the Unsigned decimal number (negative)   
printf("\n%%f = %f ",$number1); // It returns the Floating-point number (local settings aware)  
printf("\n%%F = %F ",$number1); // It returns the Floating-point number (not local settings aware)  
printf("\n%%g = %g ",$number1); // It returns the Shorter of %e and %f  
printf("\n%%G = %G ",$number1); // It returns the Shorter of %E and %f   
printf("\n%%o = %o ",$number2); // It returns the Octal number  
?> 

Output

%u = 129784563 
%u = 18446744073579767053 
%f = 129784563.000000 
%F = 129784563.000000 
%g = 1.29785e+8 
%G = 1.29785E+8 
%o = 1777777777777020722415