PHP Function Reference

PHP fprintf() Function



The PHP fprintf() function writes a string produced according to the specified format to the specified stream. If format includes format specifiers (sub-sequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.

After the format parameter, the function expects additional arguments at least as many as the number of format specifiers in the format string.

Syntax

fprintf(stream, format, arguments)

Parameters

stream Required. Specify the file pointer to write to. It must be valid, and must point to a file successfully typically opened by using fopen() function.
format

Required. Specify the format string. The possible values of %specifier are:

%specifierDescription
%%Returns a percent sign
%bBinary number
%cThe character according to the ASCII value
%dSigned decimal number (negative, zero or positive)
%eScientific notation using a lowercase (e.g. 1.2e+3)
%EScientific notation using a uppercase (e.g. 1.2E+3)
%uUnsigned decimal number (zero or positive)
%fFloating-point number (locale setting aware)
%FFloating-point number (non-locale setting aware)
%gShorter of %e and %f
%GShorter of %E and %f
%hShorter of %F. Available as of PHP 8.0.0
%HShorter of %E and %F. Available as of PHP 8.0.0
%oOctal number
%sString
%xHexadecimal number (lowercase letters)
%XHexadecimal number (uppercase letters)

Additional format values can be placed between the % and the specifier (e.g. %.3f). The possible values which can be placed in between are:

  • Flags:
    • - : Left-justify within the given field width. Right justification is the default.
    • + : Prefix positive numbers with a plus sign +. By default only negative are prefixed with a negative sign.
    • (space) : Pads the result with spaces. This is the default.
    • 0 : Only left-pads numbers with zeros. With s specifiers this can also right-pad with zeros.
    • '(char) : Pads the result with the character (char). Example: %'x20s (this uses "x" as padding).
  • Width: Specify an integer which tells how many characters (minimum) this conversion should result in.
  • Precision: Specify period . followed by an integer. Signifies number of decimal digits or maximum string length.
    • For e, E, f and F specifiers: It signifies the number of decimal digits (Default is 6).
    • For g, G, h and H specifiers: It signifies the maximum number of significant digits.
    • For s specifier: It specifies a cutoff point, setting maximum string length.

Note: If multiple additional format values are provided, they must be in %[flags][width][.precision]specifier order.

arguments Optional. Depending on the format string, a sequence of additional arguments should be passed in the function, each containing a value to replace a format specifiers in the format string. Number of arguments should be at least equal to the number of format specifiers in the format string. Additional arguments will be ignored by this function.

Return Value

Returns the length of the string written.

Example: different specifiers

Lets assume that we have a file called test.txt in the current working directory. In the example below, this function is used to write the formatted string in this file.

<?php
//open the file in write mode
$fp = fopen("test.txt", "w+");

$n =  5000;
$u = -5000;
$c = 66; // ASCII 66 is 'B'

//%% is used to print '%' character
fprintf($fp, "%%b = %b\n", $n); //binary 
fprintf($fp, "%%c = %c\n", $c); //ASCII character
fprintf($fp, "%%d = %d\n", $n); //integer
fprintf($fp, "%%e = %e\n", $n); //scientific notation
fprintf($fp, "%%u = %u\n", $n); //unsigned integer (+ve number)
fprintf($fp, "%%u = %u\n", $u); //unsigned integer (-ve number)
fprintf($fp, "%%f = %f\n", $n); //floating point
fprintf($fp, "%%o = %o\n", $n); //octal 
fprintf($fp, "%%s = %s\n", $n); //string 
fprintf($fp, "%%x = %x\n", $n); //hexadecimal (lowercase)
fprintf($fp, "%%X = %X\n", $n); //hexadecimal (uppercase)

fprintf($fp, "%%+d = %+d\n", $n); //sign specifier (+ve number)
fprintf($fp, "%%+d = %+d\n", $u); //sign specifier (-ve number)

//set the position to the start
rewind($fp);

//display the content of the file
while(!feof($fp)) {
  echo fgets($fp);
}

//close the file
fclose ($fp);
?>

The output of the above code will be:

%b = 1001110001000
%c = B
%d = 5000
%e = 5.000000e+3
%u = 5000
%u = 18446744073709546616
%f = 5000.000000
%o = 11610
%s = 5000
%x = 1388
%X = 1388
%+d = +5000
%+d = -5000

Example: using multiple % signs

In the example below, multiple % signs are used to place multiple additional arguments in the string written to the file.

<?php
//open the file in write mode
$fp = fopen("test.txt", "w+");

$name =  'John';
$age = 25;

fprintf($fp, "%s is %d years old.\n", $name, $age);

//using precision format
fprintf($fp, "%s is %.2f years old.\n", $name, $age);

//set the position to the start
rewind($fp);

//display the content of the file
while(!feof($fp)) {
  echo fgets($fp);
}

//close the file
fclose ($fp);
?>

The output of the above code will be:

John is 25 years old.
John is 25.00 years old.

Example: write a date string

In the example below, this function is used to write formatted date string in the file.

<?php
//open the file in write mode
$fp = fopen("test.txt", "w+");

$year =  2015;
$month = 5;
$day = 1;

//each format specifier specifies padding 
//with 0 and width of the field
fprintf($fp, "Date is: %04d-%02d-%02d", $year, $month, $day);

//set the position to the start
rewind($fp);

//display the content of the file
while(!feof($fp)) {
  echo fgets($fp);
}

//close the file
fclose ($fp);
?>

The output of the above code will be:

Date is: 2015-05-01

Example: string specifiers

Consider one more example to see how to use string specifiers with a given string.

<?php
//open the file in write mode
$fp = fopen("test.txt", "w+");

$x = 'catfish';
$y = 'many catfishes';

fprintf($fp, "[%s]\n",      $x); //standard string output
fprintf($fp, "[%10s]\n",    $x); //right-justification with spaces
fprintf($fp, "[%-10s]\n",   $x); //left-justification with spaces
fprintf($fp, "[%010s]\n",   $x); //zero-padding works on strings too
fprintf($fp, "[%'#10s]\n",  $x); //using custom padding character '#'
fprintf($fp, "[%10.8s]\n",  $y); //right-justification (8 characters cutoff)
fprintf($fp, "[%-10.8s]\n", $y); //left-justification (8 characters cutoff)

//set the position to the start
rewind($fp);

//display the content of the file
while(!feof($fp)) {
  echo fgets($fp);
}

//close the file
fclose ($fp);
?>

The output of the above code will be:

[catfish]
[   catfish]
[catfish   ]
[000catfish]
[###catfish]
[  many cat]
[many cat  ]

❮ PHP String Reference