sprintf()
function writes the formatted string into a variable.
The arg1 , arg2 , ++ parameters will be inserted at the percent sign (%) symbol in the main string. This function is executed step by step. At the first % symbol, insert arg1 , at the second % symbol, insert arg2 , and so on.
Note: If the % symbol is more than the arg parameter, you must use placeholders. The placeholder is behind the % symbol and consists of a number and "\$". See Example 2.
fprintf()
printf()
vfprintf()
vprintf()
vsprintf()
Replace the percent sign (%) symbol with a variable passed as a parameter:
<?php $number = 2 ; $str = "Shanghai" ; $txt = sprintf ( "There are %u million cars in %s." , $number , $str ) ; echo $txt ; ?>
Try it yourself
Use the format value %f:
<?php $number = 123 ; $txt = sprintf ( "%f" , $number ) ; echo $txt ; ?>
Try it yourself
Use placeholders:
<?php $number = 123 ; $txt = sprintf ( "with two decimal places: %1\$.2f <br>No decimals: %1\$u" , $number ) ; echo $txt ; ?>
Try it yourself
Demonstration of all possible format values:
<?php $num1 = 123456789 ; $num2 = - 123456789 ; $char = 50 ; // ASCII character 50 is 2 // Comment: Format value "%%" returns a percent sign echo sprintf ( "%%b = %b" , $num1 ) . "<br>" ; // Binary number echo sprintf ( "%%c = %c" , $char ) . "<br>" ; // ASCII characters echo sprintf ( "%%d = %d" , $num1 ) . "<br>" ; // Signed decimal number echo sprintf ( "%%d = %d" , $num2 ) . "<br>" ; // Signed decimal number echo sprintf ( "%%e = %e" , $num1 ) . "<br>" ; // Scientific notation (lowercase) echo sprintf ( "%%E = %E" , $num1 ) . "<br>" ; // Scientific notation (caps) echo sprintf ( "%%u = %u" , $num1 ) . "<br>" ; // Unsigned decimal number (positive) echo sprintf ( "%%u = %u" , $num2 ) . "<br>" ; // Unsigned decimal number (negative) echo sprintf ( "%%f = %f" , $num1 ) . "<br>" ; // Floating point number (depending on local settings) echo sprintf ( "%%F = %F" , $num1 ) . "<br>" ; // Floating point number (not depending on local settings) echo sprintf ( "%%g = %g" , $num1 ) . "<br>" ; // Shorter than %e and %f echo sprintf ( "%%G = %G" , $num1 ) . "<br>" ; // Shorter than %E and %f echo sprintf ( "%%o = %o" , $num1 ) . "<br>" ; // Octal number echo sprintf ( "%%s = %s" , $num1 ) . "<br>" ; // String echo sprintf ( "%%x = %x" , $num1 ) . "<br>" ; // Hexadecimal number (lowercase) echo sprintf ( "%%X = %X" , $num1 ) . "<br>" ; // Hexadecimal number (caps) echo sprintf ( "%%+d = %+d" , $num1 ) . "<br>" ; // Symbol specifier (positive) echo sprintf ( "%%+d = %+d" , $num2 ) . "<br>" ; // Symbol specifier (negative) ?>
Try it yourself
Demonstration of string specifiers:
<?php $str1 = "Hello" ; $str2 = "Hello world!" ; echo sprintf ( "[%s]" , $str1 ) . "<br>" ; echo sprintf ( "[%8s]" , $str1 ) . "<br>" ; echo sprintf ( "[%-8s]" , $str1 ) . "<br>" ; echo sprintf ( "[%08s]" , $str1 ) . "<br>" ; echo sprintf ( "[%'*8s]" , $str1 ) . "<br>" ; echo sprintf ( "[%8.8s]" , $str2 ) . "<br>" ; ?>
Try it yourself
sprintf ( format , arg1 , arg2 , arg ++ )
parameter | describe |
---|---|
format |
Required. Specifies the string and how to format the variables therein. Possible format values:
Additional format value. Necessarily placed between % and letters (for example %.2f):
Note: If you use multiple format values above, they must be used in the above order. |
arg1 | Required. Specifies the parameters inserted into the first % symbol in the format string. |
arg2 | Optional. Specifies the parameter inserted into the second % symbol in the format string. |
arg++ | Optional. Specifies the parameters inserted into the third and fourth % symbols in the format string. |