fprintf – PHP String Functions

Syntax :

fprintf ( stream_handle, string_format, arg1, arg2, arg3… );

Description :

fprintf() function writes a formatted string to a stream. Stream specified by stream_handle.

Write a string produced according to string_format to the stream resource specified by stream_handle.

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.

Note : Stream can be a file or database.

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description

stream_handle, Required, resource , It Specifies where to write/output the string

string_format, Required, String , Specifies the string and how to format the variables in it.

arg1, Required, Mixed , Required. The argument to be inserted at the first %-sign in the format string

arg2, Optional, Mixed , Optional. The argument to be inserted at the second %-sign in the format string

arg3, Optional, Mixed , The argument to be inserted at the third and fourth and so on etc. %-sign in the format string.


String format types ::

It is a Required parameter and 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.

Output :

It returns the length of the string written to database or file.

Or words length of the output string.

Related articles :  number_format(), printf(), sprintf(), sscanf(), fscanf(), vsprintf().

fprintf() – PHP Functions Example 1 : Write one text line to a file.
$numb = 10;
$str = "PHP";
$file = fopen("test.txt","w");
echo fprintf($file,"There are %u pages tutorial of my subject %s.",$num,$str);

Output of above code in the browser is as below. The following text will be written to the file “test.txt”:

There are 10 pages tutorial of my subject PHP.

fprintf() – PHP Functions Example 2 : Use of Placeholders
$number = 167;
$file = fopen("test.txt","w");
fprintf($file,"With 2 decimals: %1\$.2f
\nWith no decimals: %1\$u",$number);

The following code in written in the file “test-frontf.txt” :

With 2 decimals: 167.00
With no decimals: 167

fprintf() – PHP Functions Example 3 : Use of all the possible formats for the values
$n = 45645654;
$u = -45645654;
$c = 66; // ASCII 65 is 'B'

// notice the double %%, this prints a literal '%' character
printf("%%b = '%b'\n", $n); // binary representation
printf("%%c = '%c'\n", $c); // print the ascii character, same as chr() function
printf("%%d = '%d'\n", $n); // standard integer representation
printf("%%e = '%e'\n", $n); // scientific notation
printf("%%u = '%u'\n", $n); // unsigned integer representation of a positive integer
printf("%%u = '%u'\n", $u); // unsigned integer representation of a negative integer
printf("%%f = '%f'\n", $n); // floating point representation
printf("%%o = '%o'\n", $n); // octal representation
printf("%%s = '%s'\n", $n); // string representation
printf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case)
printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case)

printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integer
printf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer

The following code in written in the file “test-frontf.txt” :

%b = ‘10101110000111111101010110’
%c = ‘B’
%d = ‘45645654’
%e = ‘4.564565e+7’
%u = ‘45645654’
%u = ‘18446744073663905962’
%f = ‘45645654.000000’
%o = ‘256077526’
%s = ‘45645654’
%x = ‘2b87f56’
%X = ‘2B87F56’
%+d = ‘+45645654’
%+d = ‘-45645654’
%b = 111010110111100110100010101

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *