Php vfprintf
From w3cyberlearnings
Contents |
PHP function vfprintf
This function writes a formatted string to output stream. (File or database)
Syntax vfprintf
- stream: File handler
- format: string product according to the format.
- array: array argument to be inserted which specifies in the format.
vfprintf(stream,format,array)
format
- % - returns a percent sign.
- b - is an integer and display it as a binary number.
- c - is an integer and display it as a ASCII value.
- d - is an integer and display as a signed decimal number.
- e - is scientific notation with lowercase letter e(e.g. 1.2e+2).
- E - is scientific notation with capital letter E(e.g.1.2E+2).
- u - is an integer, and display as an unsigned decimal number.
- f- is a float, and display as a floating-point number. (local aware)
- F - is a float, and display as a floating-point number (non-locale aware).
- g - is shorter of %e and %f.
- G - is shorter of %E and %f.
- o - is an integer, and display as an octal number.
- s - is string and display as a string.
- x - is an integer and display as a hexadecimal number (with lowercase letters).
- X - is an integer and display as a hexadecimal number (with uppercase letters).
Return
- Write content with a formatted string to the stream.
Example 1
<?php if (!($fp = fopen("test2.txt", 'w'))) { return; } $arr = array('name' => 'Jamey'); vfprintf($fp, "Welcome %s to w3cyberlearnings.com", $arr); ?>
Output:create a file call test2.txt and save content
Welcome Jamey to w3cyberlearnings.com
Example 2
<?php $name = "Sam"; $age = 13; $file = fopen("test23.txt", "w"); vfprintf($file, "%s is %u year old", array($name, $age)); // open file and read $file_handler = fopen("test23.txt", 'r'); $file_data = fread($file_handler, 1024); fclose($file_handler); echo ($file_data); ?>
Output: create a file call test23.txt and save content to the file
Sam is 13 year old