PHP vfprintf() Function
The PHP vfprintf() function writes a string produced according to the specified format to the specified stream. If format includes format specifiers (sub-sequences beginning with %), the values from array argument following format are formatted and inserted in the resulting string replacing their respective specifiers.
After the format parameter, the function expects additional argument as an array containing values at least as many as the number of format specifiers in the format string.
Syntax
vfprintf(stream, format, arguments)
Parameters
stream |
Required. Specify the file pointer to write to. It must be valid, and must point to a file successfully typically opened by using fopen() function. | ||||||||||||||||||||||||||||||||||||
format |
Additional format values can be placed between the % and the specifier (e.g. %.3f). The possible values which can be placed in between are:
Note: If multiple additional format values are provided, they must be in %[flags][width][.precision]specifier order. | ||||||||||||||||||||||||||||||||||||
arguments |
Required. Depending on the format string, specify an array containing values to replace the format specifiers in the format string. Number of arguments should be at least equal to the number of format specifiers in the format string. Additional arguments will be ignored by this function. |
Return Value
Returns the length of the outputted string.
Example: different specifiers
Lets assume that we have a file called test.txt in the current working directory. In the example below, this function is used to write the formatted string in this file.
<?php //open the file in write mode $fp = fopen("test.txt", "w+"); $n = array(5000); $u = array(-5000); $c = array(66); // ASCII 66 is 'B' //%% is used to print '%' character vfprintf($fp, "%%b = %b\n", $n); //binary vfprintf($fp, "%%c = %c\n", $c); //ASCII character vfprintf($fp, "%%d = %d\n", $n); //integer vfprintf($fp, "%%e = %e\n", $n); //scientific notation vfprintf($fp, "%%u = %u\n", $n); //unsigned integer (+ve number) vfprintf($fp, "%%u = %u\n", $u); //unsigned integer (-ve number) vfprintf($fp, "%%f = %f\n", $n); //floating point vfprintf($fp, "%%o = %o\n", $n); //octal vfprintf($fp, "%%s = %s\n", $n); //string vfprintf($fp, "%%x = %x\n", $n); //hexadecimal (lowercase) vfprintf($fp, "%%X = %X\n", $n); //hexadecimal (uppercase) vfprintf($fp, "%%+d = %+d\n", $n); //sign specifier (+ve number) vfprintf($fp, "%%+d = %+d\n", $u); //sign specifier (-ve number) //set the position to the start rewind($fp); //display the content of the file while(!feof($fp)) { echo fgets($fp); } //close the file fclose ($fp); ?>
The output of the above code will be:
%b = 1001110001000 %c = B %d = 5000 %e = 5.000000e+3 %u = 5000 %u = 18446744073709546616 %f = 5000.000000 %o = 11610 %s = 5000 %x = 1388 %X = 1388 %+d = +5000 %+d = -5000
Example: using multiple % signs
In the example below, multiple % signs are used to place multiple additional arguments in the string written to the file.
<?php //open the file in write mode $fp = fopen("test.txt", "w+"); $info = array('John', 25); vfprintf($fp, "%s is %d years old.\n", $info); //using precision format vfprintf($fp, "%s is %.2f years old.\n", $info); //set the position to the start rewind($fp); //display the content of the file while(!feof($fp)) { echo fgets($fp); } //close the file fclose ($fp); ?>
The output of the above code will be:
John is 25 years old. John is 25.00 years old.
Example: write a date string
In the example below, this function is used to write formatted date string in the file.
<?php //open the file in write mode $fp = fopen("test.txt", "w+"); $date1 = array(2015, 5, 1); $date2 = '2018-10-25'; //each format specifier specifies padding //with 0 and width of the field vfprintf($fp, "%04d-%02d-%02d\n", $date1); vfprintf($fp, "%04d-%02d-%02d", explode('-', $date2)); //set the position to the start rewind($fp); //display the content of the file while(!feof($fp)) { echo fgets($fp); } //close the file fclose ($fp); ?>
The output of the above code will be:
2015-05-01 2018-10-25
Example: string specifiers
Consider one more example to see how to use string specifiers with a given string.
<?php //open the file in write mode $fp = fopen("test.txt", "w+"); $x = array('catfish'); $y = array('many catfishes'); vfprintf($fp, "[%s]\n", $x); //standard string output vfprintf($fp, "[%10s]\n", $x); //right-justification with spaces vfprintf($fp, "[%-10s]\n", $x); //left-justification with spaces vfprintf($fp, "[%010s]\n", $x); //zero-padding works on strings too vfprintf($fp, "[%'#10s]\n", $x); //using custom padding character '#' vfprintf($fp, "[%10.8s]\n", $y); //right-justification (8 characters cutoff) vfprintf($fp, "[%-10.8s]\n", $y); //left-justification (8 characters cutoff) //set the position to the start rewind($fp); //display the content of the file while(!feof($fp)) { echo fgets($fp); } //close the file fclose ($fp); ?>
The output of the above code will be:
[catfish] [ catfish] [catfish ] [000catfish] [###catfish] [ many cat] [many cat ]
❮ PHP String Reference