PHP fprintf Function

What is fprintf Function?


This function is used to write a formatted string to a specified output stream.


fprintf(resource,format, args)

In the above syntax "resource" specifies where to output, "format" specifies the formatting to be used, "args" specifies the arguments passed.

The following are some of the parameters of "format" option used in this function
%% Returns a percent sign
%b Binary number
%c The character according to the ASCII value
%d Signed decimal number
%e Scientific notation (e.g. 1.2e+2)
%u Unsigned decimal number
%f Floating-point number (local settings aware)
%F Floating-point number (not local settings aware)
%o Octal number
%s String
%x Hexadecimal number in lower case
%X Hexadecimal number in upper case

Usually in the "fprintf" function based on the "%" the arguments are displayed accordingly

Example :

$str = "Hi";
$num = 456;
$file = fopen("test.txt","w");
echo fprintf($file,"%s how are you. I owe you %u" rupees,$str,$num);

Result in test.txt:

Hi how are you, I owe you 456 rupees

In the above example the arguments "$str", "$num" will be added to the string based on the formatting specified, the output is added to the file "file.txt"

PHP Topics

Ask Questions

Ask Question