The sprintf() 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 “\$”.
[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 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:
You may also like to see the examples of printf() function.
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.…