Home >>PHP String Functions >PHP vfprintf() Function
PHP vfprintf() function is used to convert or transform a formatted string to any specific output stream like file or database. It get the input arguments in the form of an array. The elements of that array will be inserted along with percent (%) signs in the given input string.
Syntax:
vfprintf($stream,$format,$array);
Parameter | Description |
---|---|
stream | This is a required parameter. This parameter defines where to write or output the string. |
format | This is a required parameter. This parameter defines the string and how to format the variables in it. |
array | This is a required parameter. This parameter defines an array with arguments to be inserted at the % signs in the format string. |
Here is an example of vfprintf() function in PHP:
<html> <body> <?php $name = "Jerry"; $age = 21; $file = fopen("jerry.txt","w"); echo vfprintf($file,"My name is %s and i'm %u years old.",array($name,$age)); ?> </body> </html>
Here is an another example of different formats in vfprintf() function in PHP:
<html> <body> <?php $num1 = 12345; $num2 = -12345; printf("%%b = %b <br>",$num1); // Binary number printf("%%d = %d <br>",$num1); // Signed decimal number printf("%%d = %d <br>",$num2); // Signed decimal number printf("%%u = %u <br>",$num1); // Unsigned decimal number printf("%%u = %u <br>",$num2); // Unsigned decimal number printf("%%f = %f <br>",$num1); // Floating-point number printf("%%o = %o <br>",$num1); // Octal number printf("%%s = %s <br>",$num1); // String printf("%%x = %x <br>",$num1); // Hexadecimal number ?> </body> </html>