The fprintf() function writes a formatted string to a specified output stream.
PHP fprintf() Function has the following syntax.
fprintf(stream,format,arg1,arg2,arg++)
Parameter | Is Required | Description |
---|---|---|
stream | Required. | Where to write/output the string |
format | Required. | String and how to format the variables in it. |
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 |
Possible format values:
Additional format values. These are placed between the % and the letter (example %.2f):
Format | Meaning |
---|---|
+ | 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.
PHP fprintf() Function returns the length of the written string.
printf(), sprintf(), vprintf(), vsprintf() and vfprintf()
Write some text to a text file named "test.txt":
<?php
$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.
Use of placeholders:
<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"With 2 decimals: %1\$.2f
\nWith no decimals: %1\$u",$number);
?>
Using printf() to demonstrate all possible format values:
<?php/* w w w. j ava2s. 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.