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   

Related Topics

PHP str_ireplace() Function

PHP str_ireplace() Function The PHP str_ireplace() function in PHP replaces some characters with some other characters in a string. Syntax str_ireplace ( mixed $search , mixed  $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter specifies the value being searched for. replace(required)- This parameter represents the replacement value...

1 minute read.

PHP Echo And Print

In PHP, echo and print is used to get output data to the screen but there is some difference, between echo and print. PHP echo PHP echo is used to display the...

1 minute read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP Switch Statement with Example

PHP switch statement The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression. Syntax: switch(n) {             case1: statement if n=case1; break;             case2:             statement if n=case2; break;             case3:            ...

3 minutes read.

PHP str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

2 minutes read.

PHP str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

1 minute read.

PHP htmlspecialchars_decode() Function

PHP htmlspecialchars_decode() Function The htmlspecialchars_decode() function in PHP convert some predefined HTML entities to characters. The some predefined HTML entities that will be decoded are as follows: &amp; OR & (ampersand)&quot; OR " (double quote)&#039;...

2 minutes read.

PHP Variables

PHP Variable is the name of memory location used to hold the data.  In PHP, variable starts with $ symbol. Rules for PHP Variables. Variable starts with $ sign. Variable name...

2 minutes read.

PHP strripos() Function

PHP strripos() Function The strripos() function in PHP finds the position of the last occurrence of a substring in the specified string. It is a case-insensitive and binary-safe function. Syntax strripos ( string $haystack , mixed $needle [, int $offset ] ) Parameter haystack(required)- This parameter signifies the...

1 minute read.

PHP array_chunk() Function

PHP array_chunk() Function The array_chunk() function in PHP is used to split an array into chunks where the last chunk may contain less than size elements. Syntax array_chunk ( array $array , int $size [, bool $preserve_keys ]...

1 minute read.

PHP quoted_printable_encode() Function

PHP quoted_printable_encode() Function The quoted_printable_encode() function in PHP converts a 8 bit string to a quoted-printable string. This function is similar to imap_8bit(), except this one does not require the IMAP module to work. Syntax quoted_printable_encode ( string $str ) Parameter str(required)- This parameter represents...

1 minute read.

PHP Array Functions

There are various array functions in PHP. They are used to access and manipulate the elements ofan array. Following are functions of array. Array Function Description array() It is used to create an array. array_change_key_case() It is...

3 minutes read.

PHP array_key_last() function

The array_key_last () function in PHP gets the last key of an array if the specified array is not empty. Syntax array_key_last ( array $array )  Parameter array(required)- This parameter signifies the input array. Return This function returns the last key of the...

1 minute read.

PHP convert_uuencode() Function

PHP convert_uuencode() Function The convert_uuencode() function in PHP is used to encode a string and returns the uuencoded data. Syntax convert_uuencode ( string $data ) Parameter data(required)- This parameter represents the data to be encoded. Return This function returns the uuencoded data or FALSE on failure. Example 1 <?php //...

2 minutes read.

PHP array_pop() Function

PHP array_pop() Function The array_pop() function in PHP pops or removes the last element from the array shortening the array by one element. Syntax array_pop ( array &$array ) Parameter array(required)- This parameter represents the input array to get the value...

1 minute read.

PHP array_flip() function

PHP array_flip() function The array_flip() function in PHP exchanges all keys with their associated values in an array and returns an array in flip order. If the specified value has several occurrences, the latest key will be...

1 minute read.

PHP str_replace() Function

PHP str_replace() Function The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive. Syntax str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter signifies the value to search. replace(required)-...

1 minute read.

PHP chr() Function

PHP chr() Function The chr() function in PHP generates a single byte string from a number and returns a one-character string containing the character as specified by the unsigned integer. Syntax chr(bytevalue);    Parameter bytevalue(required)- This...

1 minute read.

PHP sizeof() Function

PHP sizeof() Function The sizeof() function in counts all elements and return the size of the array. This function is an alias of count(). Syntax sizeof( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)-...

1 minute read.

PHP quotemeta() Function

PHP quotemeta() Function The quotemeta() function in PHP quotes the meta characters and returns a string with a backslash character (\) before some predefine characters. The predefined characters are as follows: period ...

1 minute read.