PHP fprintf() Function

Last Updated:

PHP fprintf()

The fprintf() function writes a formatted string to a specified output stream.

Warning: The c type specifier ignores padding and width.


PHP Compiler
<?php $count = 9; $file = fopen("fruits.txt", "w"); echo fprintf($file, "I have %% apples.", $count); ?>


fprintf(handle, format, arg1, arg2, ...)

Parameter Values

handle Required Specifies a file system pointer resource that is typically created using fopen().
format Required Specifies the string and how to format the variables in it.
Possible format values:
  • %% - Returns a percent sign.
  • %b - A binary number.
  • %c - The ASCII value.
  • %d - Signed decimal number (negative, zero or positive).
  • %e - Scientific notation using a lowercase.
  • %E - Scientific notation using a uppercase.
  • %f - Floating-point number (local aware).
  • %F - Floating-point number (non-locale aware).
  • %g - shorter of %e and %f.
  • %G - shorter of %E and %f.
  • %o - An octal number.
  • %s - A string.
  • %u - Unsigned decimal number (equal to or greather than zero).
  • %x - A hexadecimal number (lowercase letters).
  • %X - A hexadecimal number (uppercase letters).
arg1 Required The argument to be added as the first %-sign in the formatted string.
arg2, .. Optional The argument to be added as the second %-sign in the formatted string and so on.

Return Value

Number Returns the length of the string written.

Share this Page

Meet the Author