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” :
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…