The printf() function output a formatted string. Output will come according to the string_format. 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 one.
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description
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 will returns the length of the outputted string.
string_format, Required, String , Specifies the string and how to format the variables in it.
The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result and conversion specifications, each of which results in fetching its own parameter. This applies to both sprintf() and printf().
Each conversion specification consists of a percent sign (%), followed by one or more of these elements, in order:
A type specifier that says what type the argument data should be treated as. Possible types:
Type Handling
Type, Specifiers
string , s
integer , d , u, c, o, x, c, o , x, X , b
double , g , G, e, E, f , F
Attempting to use a combination of the string and width specifiers with character sets that require more than one byte per character may result in unexpected results
The format string supports argument numbering/swapping.
<?php $n = 23457274; $u = -23457274; $c = 66; // ASCII 65 is 'A' // 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 ?>
Output of above code in the browser is as below:
<?php $s = 'Tutorial'; $t = 'many Tutorials'; printf("[%s]\n", $s); // standard string output printf("[%15s]\n", $s); // right-justification with spaces printf("[%-15s]\n", $s); // left-justification with spaces printf("[%015s]\n", $s); // zero-padding works on strings too printf("[%'#15s]\n", $s); // use the custom padding character '#' printf("[%15.15s]\n", $t); // left-justification but with a cutoff of 15 characters ?>
Output of above code in the browser is as below:
<?php echo $isodate = sprintf("%04d-%02d-%02d", '2019', '2', '4'); ?>
Output of above code in the browser is as below:
<?php $number = 2398675000; echo sprintf("%.3e", $number); ?>
Output of above code in the browser is as below:
<?php $strMoney1 = 34.75; $strMoney2 = 89.35; $sumMoney = $strMoney1 + $strMoney2; $formatted = sprintf("%01.2f", $sumMoney); echo $formatted; ?>
Output of above code in the browser is as below:
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…