vfprintf – PHP String Functions
Syntax :
Description :
vfprintf() function writes a formatted string to a stream. Stream specified by stream_handle.
Works similar as fprintf() but accepts an array of arguments, rather than a variable number of arguments.
Write a string produced according to string_format to the stream resource specified by stream_handle.
Note: If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and “\$”.
Parameter :
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description
stream_handle, Required, Resource , It Specifies where to write/output the string.
string_format, Required, String , Specifies the string and how to format the variables in it.
argarray, Required, Array , Required. An array with arguments to be inserted at the % signs in the format string.
[/table]
String format types ::
It is a Required parameter and Specifies the string and how to format the variables in it. 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 values. These are placed between the % and the letter (example %.2f):
- + (Forces both + and – in front of numbers. By default, only negative numbers are marked)
- ‘ (Specifies 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] (Specifies the minimum width held of to the variable value)
- .[0-9] (Specifies the number of decimal digits or maximum string length)
Output :
It returns the length of the written string.
Related articles : number_format(), printf(), sprintf(), sscanf(), fscanf(), vsprintf().
vfprintf() – PHP Functions Example 1 : Write one text line to a file.
<?php $numb = 10; $str = "PHP"; $file = fopen("test.txt","w"); echo vfprintf($file,"There are %u pages tutorial of my subject %s.",array($num,$str)); ?>
Output of above code in the browser is as below. The following text will be written to the file “test.txt”:
There are 10 pages tutorial of my subject PHP.
vfprintf() – PHP Functions Example 2 : Use of Placeholders
<?php $number = 167; $file = fopen("test.txt","w"); vfprintf($file,"With 2 decimals: %1\$.2f \nWith no decimals: %1\$u",array($number)); ?>
The following code in written in the file “test-frontf.txt” :
With no decimals: 167
fprintf() – PHP Functions Example 3 : Use of all the possible formats for the values
<?php $n = 45645654; $u = -45645654; $c = 66; // ASCII 65 is 'B' // notice the double %%, this prints a literal '%' character printf("%%b = '%b'\n", $n); // binary representation printf("%%c = '%c'\n", $c); // print the ascii character, same as chr() function printf("%%d = '%d'\n", $n); // standard integer representation printf("%%e = '%e'\n", $n); // scientific notation printf("%%u = '%u'\n", $n); // unsigned integer representation of a positive integer printf("%%u = '%u'\n", $u); // unsigned integer representation of a negative integer printf("%%f = '%f'\n", $n); // floating point representation printf("%%o = '%o'\n", $n); // octal representation printf("%%s = '%s'\n", $n); // string representation printf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case) printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case) printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integer printf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer ?>
The following code in written in the file “test-frontf.txt” :
%c = ‘B’
%d = ‘45645654’
%e = ‘4.564565e+7’
%u = ‘45645654’
%u = ‘18446744073663905962’
%f = ‘45645654.000000’
%o = ‘256077526’
%s = ‘45645654’
%x = ‘2b87f56’
%X = ‘2B87F56’
%+d = ‘+45645654’
%+d = ‘-45645654’
%b = 111010110111100110100010101