PHP vfprintf() function
Example
Write the text to the text file named "test.txt":
<?php $number = 9; $str = "Beijing"; $file = fopen("test.txt", "w"); echo vfprintf($file,"There are 쥕lion bicycles in %s.",array($number,$str)); ?>
The above code will output:
40
The following text will be written to the file "test.txt":
There are 9 million bicycles in Beijing.
Definition and Usage
The vfprintf() function writes the formatted string to the specified output stream (e.g., a file or database).
Unlike fprintf(), the parameters in vfprintf() are located in an array. Array elements will be inserted into the main string at the percentage (%) symbols. This function is executed step by step. At the first % symbol, the first array element is inserted, at the second % symbol, the second array element is inserted, and so on.
Comment:If the % symbol is more than arg If there are parameters, you must use placeholders. Placeholders are inserted after the % symbol and consist of a number and "\$". See example 2.
Related functions:
Syntax
vfprintf(stream,format,argarray)
Parameter | Description |
---|---|
stream | Required. Specifies where to write/output the string. |
format |
Required. Specifies the string and how to format the variables within it. Possible format values:
Additional format values. They must be placed between % and a letter (e.g., %.2f):
Comment:If multiple format specifiers are used, they must be in the order specified above. |
argarray | Required. An array with parameters that will be inserted format At the % symbol in the string. |
Technical details
Return value: | Returns the length of the string written. |
PHP version: | 5+ |
More examples
Example 1
Write text to the file:
<?php $num1 = 123; $num2 = 456; $file = fopen("test.txt", "w"); vfprintf($file, "%f%f", array($num1, $num2)); ?>
The following text will be written to the file "test.txt":
123.000000456.000000
Example 2
Using placeholders:
<?php $number = 123; $file = fopen("test.txt", "w"); vfprintf($file, "With 2 decimals: %1$.2f \nWith no decimals: %1$u", array($number)); ?>
The following text will be written to the file "test.txt":
With 2 decimals: 123.00 With no decimals: 123
Example 3
Use printf() to demonstrate all possible format values:
<?php $num1 = 123456789; $num2 = -123456789; $char = 50; // ASCII character 50 is 2 // Comment: The format value "%%" returns a percent sign printf("%%b = %%b <br>", $num1); // Binary number printf("%%c = %%c <br>", $char); // ASCII character printf("%%d = %%d <br>", $num1); // Signed decimal number printf("%%d = %%d <br>", $num2); // Signed decimal number printf("%%e = %%e <br>", $num1); // Scientific notation (lowercase) printf("%%E = %%E <br>", $num1); // Scientific notation (uppercase) printf("%鑾u <br>", $num1); // Unsigned Decimal Number (Positive) printf("%鑾u <br>", $num2); // Unsigned Decimal Number (Negative) printf("%%f = %f <br>", $num1); // Floating Point Number (Respecting local settings) printf("%%F = %F <br>", $num1); // Floating Point Number (Not respecting local settings) printf("%%g = %g <br>", $num1); // Shorter than %e and %f printf("%%G = %G <br>", $num1); // Shorter than %E and %f printf("%%o = %o <br>", $num1); // Octal Number printf("%%s = %s <br>", $num1); // String printf("%%x = %x <br>", $num1); // Hexadecimal Number (Lowercase) printf("%%X = %X <br>", $num1); // Hexadecimal Number (Uppercase) printf("%%+d = %+d <br>", $num1); // Positive Sign Indicator printf("%%+d = %+d <br>", $num2); // Negative Sign Indicator ?>