sprintf – PHP String Functions
Syntax :
Description :
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”.
- At the first % sign, arg1 is inserted,
- at the second % sign, arg2 is inserted, etc.
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 “\$”.
Parameter :
[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]
Output :
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:
- An optional sign specifier that forces a sign (- or +) to be used on a number. By default, only the – sign is used on a number if it’s negative. This specifier forces positive numbers to have the + sign attached as well.
- An optional padding specifier that says what character will be used for padding the results to the right string size. This may be a space character or a 0 (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote (‘). See the examples below.
- An optional alignment specifier that says if the result should be left-justified or right-justified. The default is right-justified; a – character here will make it left-justified.
- An optional number, a width specifier that says how many characters (minimum) this conversion should result in.
- An optional precision specifier in the form of a period (.) followed by an optional decimal digit string that says how many decimal digits should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string. Additionally, the character to use when padding a number may optionally be specified between the period and the digit.
A type specifier that says what type the argument data should be treated as. Possible types:
- % – a literal percent character. No argument is required.
- b – the argument is treated as an integer and presented as a binary number.
- c – the argument is treated as an integer and presented as the character with that ASCII value.
- d – the argument is treated as an integer and presented as a (signed) decimal number.
- e – the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less).
- E – like %e but uses uppercase letter (e.g. 1.2E+2).
- f – the argument is treated as a float and presented as a floating-point number (locale aware).
- F – the argument is treated as a float and presented as a floating-point number (non-locale aware). Available since PHP 5.0.3.
- g – shorter of %e and %f.
- G – shorter of %E and %f.
- o – the argument is treated as an integer and presented as an octal number.
- s – the argument is treated as and presented as a string.
- u – the argument is treated as an integer and presented as an unsigned decimal number.
- x – the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).
- X – the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).
Variables will be co-erced to a suitable type for the specifier :
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.
Related articles : echo(), print(), flush(), printf(), fprintf(), vprintf(), sscanf(), fscanf(), number_format().
sprintf() – PHP Functions Example 1 : zero-padded integers
<?php echo $isodate = sprintf("%04d-%02d-%02d", '2019', '2', '4'); ?>
Output of above code in the browser is as below:
sprintf() – PHP Functions Example 2 : scientific notation
<?php $number = 2398675000; echo sprintf("%.3e", $number); ?>
Output of above code in the browser is as below:
sprintf() – PHP Functions Example 3 : formatting currency
<?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.