PHP fprintf() function
Example
Write the text to the text file named "test.txt":
<?php $number = 9; $str = "Beijing"; $file = fopen("test.txt", "w"); echo fprintf($file,"There are 쥕lion bicycles in %s.",$number,$str); ?>
Output of the above code:
40
The following text will be written to the file "test.txt":
There are 9 million bicycles in Beijing.
Definition and Usage
The fprintf() function writes the formatted string to the specified output stream (for example: file or database).
arg1,arg2,arg++ Parameters will be inserted into the percentage (%) symbol at the main string. The function is executed step by step. Insert arg1, insert arg2, and so on.
Comment:If there are more than arg If there are parameters, you must use placeholders. Placeholders are inserted after the % symbol, consisting of numbers and "\$". See example 2.
Related functions:
Syntax
fprintf(stream,format,arg1,arg2,arg++)
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:
The additional format value. It must be placed between % and a letter (for example %.2f):
Comment:If multiple additional format specifiers are used, they must be used in the order specified above. |
arg1 | Required. Specifies the insertion point format The parameter at the first %% symbol in the string. |
arg2 | Optional. Specifies the insertion point format The parameter at the second %% symbol in the string. |
arg++ | Optional. Specifies the insertion point format The parameters at the third, fourth, etc. %% symbols in the string. |
Technical details
Return value: | Returns the length of the written string. |
PHP version: | 5+ |
More examples
Example 1
Write text to file:
<?php $number = 123; $file = fopen("test.txt", "w"); fprintf($file, "%%f", $number); ?>
The following text will be written to the file "test.txt":
123.000000
Example 2
Use placeholders:
<?php $number = 123; $file = fopen("test.txt", "w"); fprintf($file, "Two decimal places: %%f", $number); \nNo decimal: %1$u ?>
The following text will be written to the file "test.txt":
Two decimal places: 123.00 No decimal: 123
Example 3
Use printf() to demonstrate all possible format specifiers:
<?php $num1 = 123456789; $num2 = -123456789; $char = 50; // ASCII character 50 is 2 // Comment: The format specifier "%%" 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 (Affected by local settings) printf("%%F = %F <br>", $num1); // Floating Point Number (Not affected by 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 ?>