PHP fprintf() Function

You are Here:

PHP fprintf()

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

Warning: The c type specifier ignores padding and width.

Example

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

Output

23

The following text is written to the file 'fruit.txt'

I have 9.000000 apples.

Syntax

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

Parameter Values

ValueTypeExplanation
handleRequiredSpecifies a file system pointer resource that is typically created using fopen().
formatRequiredSpecifies 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).
arg1RequiredThe argument to be added as the first %-sign in the formatted string.
arg2, ..OptionalThe argument to be added as the second %-sign in the formatted string and so on.

Return Value

ValueExplanation
NumberReturns the length of the string written.

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on PHP with examples for quick and easy learning.

We are working to cover every Single Concept in PHP.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author