Current Location: Home> Function Categories> sprintf

sprintf

Returns the formatted string
Name:sprintf
Category:String
Programming Language:php
One-line Description:Write the formatted string into the variable.

Definition and usage

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.

Related functions:

  • fprintf()
  • printf()
  • vfprintf()
  • vprintf()
  • vsprintf()

Example

Example 1

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

Example 2

Use the format value %f:

 <?php
$number = 123 ;
$txt = sprintf ( "%f" , $number ) ;
echo $txt ;
?>

Try it yourself

Example 3

Use placeholders:

 <?php
$number = 123 ;
$txt = sprintf ( "with two decimal places: %1\$.2f
<br>No decimals: %1\$u" , $number ) ;
echo $txt ;
?>

Try it yourself

Example 4

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

Example 5

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

grammar

 sprintf ( format , arg1 , arg2 , arg ++ )
parameter describe
format

Required. Specifies the string and how to format the variables therein.

Possible format values:

  • %% - Returns a percent sign
  • %b - binary number
  • %c - Characters corresponding to ASCII value
  • %d - Decimal number containing positive and negative signs (negative number, 0, positive number)
  • %e - Use lowercase scientific notation (e.g. 1.2e+2)
  • %E - Scientific notation using capitals (e.g. 1.2E+2)
  • %u - Decimal number without signs (greater than or equal to 0)
  • %f - Floating point number (local setting)
  • %F - Floating point number (non-local setting)
  • %g - shorter %e and %f
  • %G - Shorter %E and %f
  • %o - octal number
  • %s - string
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (caps)

Additional format value. Necessarily placed between % and letters (for example %.2f):

  • + (Present + or - before the number to define the positive and negative nature of the number. By default, only negative numbers are marked, and positive numbers are not marked)
  • ' (Specify what to use as padding, default is a space. It must be used with the width specifyer. For example: %'x20s (use "x" as padding))
  • - (Left adjustment variable value)
  • [0-9] (Specify the minimum width of the variable value)
  • .[0-9] (Specify the number of decimal places or maximum string length)

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.