PHP fprintf() Function

Definition

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

Syntax

PHP fprintf() Function has the following syntax.

fprintf(stream,format,arg1,arg2,arg++)

Parameter

ParameterIs RequiredDescription
streamRequired.Where to write/output the string
formatRequired.String and how to format the variables in it.
arg1Required.The argument to be inserted at the first %-sign in the format string
arg2Optional.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

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

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

FormatMeaning
+Forces both + and - in front of numbers. By default, only negative numbers are marked
'Sets 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]Sets the minimum width held of to the variable value
.[0-9]Sets the number of decimal digits or maximum string length

Multiple additional format values must be in the same order as above.

Return

PHP fprintf() Function returns the length of the written string.

printf(), sprintf(), vprintf(), vsprintf() and vfprintf()

Example 1

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


<?php//from  w  w  w . java2s. c  o  m
$number = 1;
$str = "PHP";
$file = fopen("test.txt","w");
echo fprintf($file,"There are %u million developers using %s.",$number,$str);
echo fprintf($file,"%f",$number);


?>

The code above generates the following result.

Example 2

Use of placeholders:


<?php//  w  w w  .j  a v a 2s. c  om
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"With 2 decimals: %1\$.2f
\nWith no decimals: %1\$u",$number);
?>

Example 3

Using printf() to demonstrate all possible format values:


<?php/*  w  ww. j  av a 2 s .c o  m*/
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // The ASCII Character 50 is 2

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

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions