PHP fprintf() Function

What is PHP fprintf() function?

If you want to write one or more formatted strings to an output stream such as file or database, use fprintf() function. For example, you want to add a plus or minus sign in front of a number like +100 or -100. Or, you want to add two zeros after decimal point like 200.00. You can do all these with this function.

Syntax:

fprintf(stream, format, argument1, argument2, …)

Parameters:

The function has 2 required parameters and 1 optional parameter-

stream (Required): This specifies the output stream to where the formatted string will be written.

format (Required): In this parameter, mention the string that you want to format. It contains zero or more placeholders. Each placeholder defines a rule (or conversion specification) to format the value that will be inserted in it by an argument.

argument1, argument2, … (Optional): It specifies an array whose elements act as arguments.Elements are the values that are to be formatted by the rules (or conversion specification) set in the placeholders.

How fprintf() function works:

Each placeholder’s rule (or conversion specification) is replaced by each array element from the argument. The 1st rule is replaced by the 1st array element, the 2nd rule is replaced by the 2nd array element and so on. And, at last, the function sends this to an output stream.

Syntax of a conversion specification:

A conversion specification has the following syntax-

%[argnum$][flags][width][.precision]specifier.

  • %: Each conversion specification starts with percentage sign, %.
  • argnum: it is an integer followed by the dollar sign $ that tells which argument number to be used. Ex. $1 means first argument, $2 means second argument. So, you can change the regular order of the arguments that are to be used in the placeholders. Or you can repeat arguments if you wish.
  • flags: A flag consists of the following characters-
    It left-justify within the given field width. See Example 1 (line 2). Right-justification is the default.
    +It adds plus sign (+) in front of a positive number. See example 1 (line 3).
    (space)It pads the result with spaces. See example 1 (line 4).
    0It adds zero at the left of a number. If you use s specifier, it adds 0 at right too. See example 1 (line 4)
    ‘(char)It pads the result with the character (char). See example 1 (line 5)
  • width: After conversion, how many characters (minimum) should be produced is indicated by the width which is an integer number. 1 due.
  • .precision: It is a combination of a period followed by a integer like (.2) or a period followed by * (.*). It’s meaning depends on the specifier to be used next.  it Is very useful for floating point formatting.
    For e, E, f and F specifiers:When it is used with these specifiers, it tells how many digits to be displayed after decimal number (6 is the default).
    For g, G, h and H specifiers:When it is used with these specifiers, it tells how many significant digits to be displayed.
    For s specifierWhen it is used with s specifiers, it tells after how many characters the string will be cutoff.
  • specifier: This is a single character that indicates by which type the argument is treated and presented.
    %It displays a percent sign %.
    bIt treats a parameter (argument1, argument2, …) as a integer and displays it as binary number.
    cIt treats a parameter (argument1, argument2, …) as a integer and displays it as an ASCII character equivalent value.
    dIt treats a parameter (argument1, argument2, …) as a integer and displays it as a signed decimal number.
    eIt treats a parameter (argument1, argument2, …) as a scientific notation and displays it in lowercase.
    EIt treats a parameter (argument1, argument2, …) as a scientific notation and displays it in uppercase.
    fIt treats a parameter (argument1, argument2, …) as a float and displays it as a floating-point number (locale setting dependent)
    FIt treats a parameter (argument1, argument2, …) as a float and displays it as a floating-point number (not locale setting dependent)
    gGeneral format. It uses e and f
    GGeneral format like g, but, It uses E and f.
    hGeneral format like g, but, It uses F. Available at PHP 8.0.0.
    HGeneral format like g, but, It uses E and F. Available at PHP 8.0.0.
    oIt treats a parameter (argument1, argument2, …) as a integer and displays it as an octal number.
    sIt treats a parameter (argument1, argument2, …) as a string and displays it as a string.
    uIt treats a parameter (argument1, argument2, …) as a integer and displays it as an unsigned decimal number.
    xIt treats a parameter (argument1, argument2, …) as a integer and displays it as a lowercase hexadecimal number.
    XIt treats a parameter (argument1, argument2, …) as a integer and displays it as a uppercase hexadecimal number.

Return Values:

The function returns the length of the formatted string written.

Examples:

Example 1:

<?php
$employee_number = 98;
$handle = fopen("attendance.txt", "w") or die("Unable to open file!");
fprintf($handle,"%u employees are working today", $employee_number);
?>

Explanation:

The program will generate file named attendance.txt and it contains the string – “98 employees are working today”.

Difference between fprintf() and printf() function:

Both functions are identical except the printf() function sends output to the screen and fprints() writes output to an output stream.

PHP Version Support:

PHP5, PHP7, PHP8

Summary: PHP fprintf() Function

fprintf() is another very useful and powerful built-in PHP function. Use this function when you need the formatted string to an output stream like file or database.

Reference:

https://www.php.net/manual/en/function.fprintf.php