PHP fprintf() Function


The fprintf() function writes a formatted string to the specified output stream. This function formats a string for the specified output stream (file or database).

Syntax
fprintf(stream,format,arg1,arg2,arg++)
Parameters
Parameter Description
stream (Required) Specify the string to write/output the string.
format (Required) Specifies formatting for strings and variables within them.
  • %% - Returns the percent sign
  • %b - Binary number
  • %c - Character based on ASCII value
  • %d - Signed decimal number (negative, zero, or positive)
  • %e - Scientific notation using lowercase letters
  • %E - Uppercase scientific notation
  • %u - Unsigned decimal number (greater than or equal to 0)
  • %f - Floating point number (respects local settings)
  • %F - Floating point number (local settings are not taken into account)
  • %g - Short version of %e and %f
  • %G - Shorter than %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal (lowercase)
  • %X - Hexadecimal (uppercase)
arg1 (Required) Argument to insert into first % character of format string.
arg2 (Optional) Argument to insert into second % character of format string.
arg++ (Optional) Argument to insert at the third,fourth,etc. % character in the format string.
Example
<?php
$name = "Ram";
$age = 25;
$file = fopen("sample.txt","w");
echo fprintf($file,"%s age is %u",$name,$age);
?>

Output

14

Output text will be written to the file "sample.txt"

Ram age is 25

Prev Next