Easy Tutorial
❮ Php File Upload Php Imagecolorat ❯

PHP fprintf() Function

PHP String Reference Manual

Example

Write some text to the text file named "test.txt":

The above code will output:

The following text will be written to the file "test.txt":


Definition and Usage

The fprintf() function writes a formatted string to a specified output stream (e.g., file or database).

The arg1, arg2, ++ parameters will be inserted at the percent (%) symbol in the main string. The function executes step by step. At the first % symbol, arg1 is inserted, at the second % symbol, arg2 is inserted, and so on.

Note: If there are more % symbols than arg parameters, you must use placeholders. Placeholders are inserted after the % symbol and consist of a number and "$". See Example 2.

Tip: Related functions: printf(), sprintf(), vprintf(), vsprintf(), and vfprintf()


Syntax

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: %% - Returns a percent sign %<br> %b - Binary number<br> %c - Character corresponding to the ASCII value<br> %d - Signed decimal number (negative, zero, positive)<br> %e - Lowercase scientific notation (e.g., 1.2e+2)<br> %E - Uppercase scientific notation (e.g., 1.2E+2)<br> %u - Unsigned decimal number (greater than or equal to 0)<br> %f - Floating point number (locale aware)<br> %F - Floating point number (non-locale aware)<br> %g - Shorter of %e and %f<br> %G - Shorter of %E and %f<br> %o - Octal number<br> %s - String<br> %x - Hexadecimal number (lowercase letters)<br> %X - Hexadecimal number (uppercase letters) Additional format values. Must be placed between % and the letter (e.g., %.2f): + (Adds a + or - sign before the number. By default, only negative numbers are marked, positive numbers are not marked)<br> ' (Specifies what to use as padding. Default is a space. It must be used with a width specifier. For example: %'x20s (uses "x" as padding))<br> - (Left-justifies the variable value)<br> [0-9] (Specifies the minimum width of the variable value)<br> .[0-9] (Specifies the number of decimal places or the maximum string length) Note: If multiple format values are used, they must be used in the order shown above, without rearranging.
arg1 Required. Specifies the argument to insert at the first % symbol in the format string.
arg2 Optional. Specifies the argument to insert at the second % symbol in the format string.
arg++ Optional. Specifies the argument to insert at the third, fourth, etc., % symbol in the format string.

Technical Details

Return Value: Returns the length of the written string.
PHP Version: 5+
--- ---

More Examples

Example 1

Write some text to a file:

The following text will be written to the file "test.txt":

Example 2

Using placeholders:

The following text will be written to the file "test.txt":

Example 3

Using printf() to demonstrate all possible format values:


❮ Php File Upload Php Imagecolorat ❯