fprintf() function writes a formatted string to a stream. Stream specified by stream_handle.
Write a string produced according to string_format to the stream resource specified by stream_handle.
The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main string.
This function works “step-by-step”.
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.
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description
stream_handle, Required, resource , It Specifies where to write/output the string
string_format, Required, String , Specifies the string and how to format the variables in it.
arg1, Required, Mixed , Required. The argument to be inserted at the first %-sign in the format string
arg2, Optional, Mixed , Optional. The argument to be inserted at the second %-sign in the format string
arg3, Optional, Mixed , The argument to be inserted at the third and fourth and so on etc. %-sign in the format string.
[/table]
It is a Required parameter and 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):
It returns the length of the string written to database or file.
Or words length of the output string.
<?php $numb = 10; $str = "PHP"; $file = fopen("test.txt","w"); echo fprintf($file,"There are %u pages tutorial of my subject %s.",$num,$str); ?>
Output of above code in the browser is as below. The following text will be written to the file “test.txt”:
There are 10 pages tutorial of my subject PHP.
<?php $number = 167; $file = fopen("test.txt","w"); fprintf($file,"With 2 decimals: %1\$.2f \nWith no decimals: %1\$u",$number); ?>
The following code in written in the file “test-frontf.txt” :
<?php $n = 45645654; $u = -45645654; $c = 66; // ASCII 65 is 'B' // notice the double %%, this prints a literal '%' character printf("%%b = '%b'\n", $n); // binary representation printf("%%c = '%c'\n", $c); // print the ascii character, same as chr() function printf("%%d = '%d'\n", $n); // standard integer representation printf("%%e = '%e'\n", $n); // scientific notation printf("%%u = '%u'\n", $n); // unsigned integer representation of a positive integer printf("%%u = '%u'\n", $u); // unsigned integer representation of a negative integer printf("%%f = '%f'\n", $n); // floating point representation printf("%%o = '%o'\n", $n); // octal representation printf("%%s = '%s'\n", $n); // string representation printf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case) printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case) printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integer printf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer ?>
The following code in written in the file “test-frontf.txt” :
When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…
In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…
In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…
In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…
When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…
In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…