Write some text to a text file named "test.txt":
The output of the code above will be:
The following text will be written to the file "test.txt":
The vfprintf() function writes a formatted string to a specified output stream (example: file or database).
Unlike fprintf(), the arguments in vfprintf(), are placed in an array. The array elements will be inserted at the percent (%) signs in the main string. This function works "step-by-step". At the first % sign, the first array element is inserted, at the second % sign, the second array element is inserted, etc.
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 "\$". See example two.
Tip: Related functions: fprintf(), printf(), sprintf(), vprintf() and vsprintf().
| Parameter | Description |
|---|---|
| stream | Required. Specifies where to write/output the string |
| format | Required. Specifies the string and how to format the
variables in it. Possible format values:
Additional format values. These are placed between the % and the letter (example %.2f):
Note: If multiple additional format values are used, they must be in the same order as above. |
| argarray | Required. An array with arguments to be inserted at the % signs in the format string |
| Return Value: | Returns the length of the written string |
|---|---|
| PHP Version: | 5+ |
Write some text to a file:
The following text will be written to the file "test.txt":
Use of placeholders:
The following text will be written to the file "test.txt":
Using printf() to demonstrate all possible format values:
Complete PHP String Reference
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The HTML5 Certificate documents your knowledge of advanced HTML5.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
Your message has been sent to W3Schools.