PHP7 Tutorial

PHP7 HOME PHP7 Intro PHP7 Install PHP7 Syntax PHP7 Variables PHP7 Echo / Print PHP7 Data Types PHP7 Strings PHP7 Constants PHP7 Operators PHP7 If...Else...Elseif PHP7 Switch PHP7 While Loops PHP7 For Loops PHP7 Functions PHP7 Arrays PHP7 Sorting Arrays PHP7 Superglobals

PHP7 Forms

PHP7 Form Handling PHP7 Form Validation PHP7 Form Required PHP7 Form URL/E-mail PHP7 Form Complete

PHP7 Advanced

PHP7 Arrays Multi PHP7 Date and Time PHP7 Include PHP7 File Handling PHP7 File Open/Read PHP7 File Create/Write PHP7 File Upload PHP7 Cookies PHP7 Sessions PHP7 Filters PHP7 Filters Advanced

MySQL Database

MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Delete Data MySQL Update Data MySQL Limit Data


PHP7 XML Parsers PHP7 SimpleXML Parser PHP7 SimpleXML - Get PHP7 XML Expat PHP7 XML DOM



PHP7 Reference

PHP7 Overview PHP7 Array PHP7 Calendar PHP7 Date PHP7 Directory PHP7 Error PHP7 Filesystem PHP7 Filter PHP7 FTP PHP7 Libxml PHP7 Mail PHP7 Math PHP7 Misc PHP7 MySQLi PHP7 Network PHP7 SimpleXML PHP7 Stream PHP7 String PHP7 XML Parser PHP7 Zip PHP7 Timezones

PHP 7 fprintf() Function

❮ PHP String Reference


Write some text to a text file named "test.txt":

$number = 9;
$str = "Beijing";
$file = fopen("test.txt","w");
echo fprintf($file,"There are %u million bicycles in %s.",$number,$str);

The output of the code above will be:


The following text will be written to the file "test.txt":

There are 9 million bicycles in Beijing.

Definition and Usage

The fprintf() function writes a formatted string to a specified output stream (example: file or database).

The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.

Note: If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and "\$". See example two.

Tip: Related functions: printf(), sprintf(), vprintf(), vsprintf() and vfprintf()


Parameter Description
stream Required. Specifies where to write/output the string
format Required. Specifies the string and how to format the variables in it.

Possible format values:

  • %% - Returns a percent sign
  • %b - Binary number
  • %c - The character according to the ASCII value
  • %d - Signed decimal number (negative, zero or positive)
  • %e - Scientific notation using a lowercase (e.g. 1.2e+2)
  • %E - Scientific notation using a uppercase (e.g. 1.2E+2)
  • %u - Unsigned decimal number (equal to or greather than zero)
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %g - shorter of %e and %f
  • %G - shorter of %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

Additional format values. These are placed between the % and the letter (example %.2f):

  • + (Forces both + and - in front of numbers. By default, only negative numbers are marked)
  • ' (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %'x20s (this uses "x" as padding)
  • - (Left-justifies the variable value)
  • [0-9] (Specifies the minimum width held of to the variable value)
  • .[0-9] (Specifies the number of decimal digits or maximum string length)

Note: If multiple additional format values are used, they must be in the same order as above.

arg1 Required. The argument to be inserted at the first %-sign in the format string
arg2 Optional. The argument to be inserted at the second %-sign in the format string
arg++ Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string

Technical Details

Return Value: Returns the length of the written string
PHP Version: 5+

More Examples


Write some text to a file:

$number = 123;
$file = fopen("test.txt","w");

The following text will be written to the file "test.txt":



Use of placeholders:

$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"With 2 decimals: %1\$.2f
\nWith no decimals: %1\$u",$number);

The following text will be written to the file "test.txt":

With 2 decimals: 123.00
With no decimals: 123


Using printf() to demonstrate all possible format values:

$num1 = 123456789;
$num2 = -123456789;
$char = 50; // The ASCII Character 50 is 2

// Note: The format value "%%" returns a percent sign
printf("%%b = %b <br>",$num1); // Binary number
printf("%%c = %c <br>",$char); // The ASCII Character
printf("%%d = %d <br>",$num1); // Signed decimal number
printf("%%d = %d <br>",$num2); // Signed decimal number
printf("%%e = %e <br>",$num1); // Scientific notation (lowercase)
printf("%%E = %E <br>",$num1); // Scientific notation (uppercase)
printf("%%u = %u <br>",$num1); // Unsigned decimal number (positive)
printf("%%u = %u <br>",$num2); // Unsigned decimal number (negative)
printf("%%f = %f <br>",$num1); // Floating-point number (local settings aware)
printf("%%F = %F <br>",$num1); // Floating-point number (not local settings aware)
printf("%%g = %g <br>",$num1); // Shorter of %e and %f
printf("%%G = %G <br>",$num1); // Shorter of %E and %f
printf("%%o = %o <br>",$num1); // Octal number
printf("%%s = %s <br>",$num1); // String
printf("%%x = %x <br>",$num1); // Hexadecimal number (lowercase)
printf("%%X = %X <br>",$num1); // Hexadecimal number (uppercase)
printf("%%+d = %+d <br>",$num1); // Sign specifier (positive)
printf("%%+d = %+d <br>",$num2); // Sign specifier (negative)
Try it Yourself »

❮ PHP String Reference