PHP sprintf() Function

Example

Replace the percentage sign (%) symbol with a variable passed as a parameter:

<?php
$number = 2;
$str = "Shanghai";
$txt = sprintf("There are 쥕lion cars in %s.", $number, $str);
echo $txt;
?>

Run Instance

Definition and usage

The sprintf() function writes the formatted string into a variable.

arg1,arg2,++ The parameters will be inserted into the main string at the percentage (%) symbols. This function is executed step by step. At the first % symbol, insert arg1, insert arg2, and so on.

Note:If there are more % symbols than arg If there are more parameters, you must use placeholders. Placeholders are located after the % symbol and consist of numbers and '\$'. See example 2.

Tip:Related functions: printf(), vprintf(), vsprintf(), fprintf(), and vfprintf()

Syntax

sprintf(format,arg1,arg2,arg++)
Parameter Description
format

Required. Specifies the string and how to format the variables within it.

Possible format values:

  • %% - Returns a percentage sign %
  • %b - Binary number
  • %c - Character corresponding to the ASCII value
  • %d - Decimal number with a sign (negative, 0, positive)
  • %e - Uses lowercase scientific notation (e.g., 1.2e+2)
  • %E - Uses uppercase scientific notation (e.g., 1.2E+2)
  • 蒝ecimal number without a sign (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 (uppercase letters)

Additional format values. Must be placed between % and the letter (e.g., %.2f):

  • + (adds + or - in front of the number to define the sign of the number. By default, only negative numbers are marked, and positive numbers are not marked)
  • ' (specifies what to use as padding, default is space. It must be used with the width specifier. For example: '%x20s' (using 'x' as padding))
  • - (left-justifies the variable value)
  • [0-9] (specifies the minimum width of the variable value)
  • .[0-9] (specifies the number of decimal places or the maximum string length)

Note:If multiple of the above format values are used, they must be used in the order listed above.

arg1 Required. Specifies what to insert into format The parameter at the first % symbol in the string.
arg2 Optional. Specifies what to insert into format The parameter at the second % symbol in the string.
arg++ Optional. Specifies what to insert into format The parameters at the third and fourth % symbols in the string.

Technical details

Return value: Returns a formatted string.
PHP Version: 4+

More examples

Example 1

Using format specifier %f:

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

Run Instance

Example 2

Using placeholders:

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

Run Instance

Example 3

Demonstration of all possible format specifiers:

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // ASCII character 50 is 2
// Comment: The format specifier "%%" returns a percent sign
echo sprintf("%%b = %b",$num1)."<br>"; // Binary number
echo sprintf("%%c = %c",$char)."<br>"; // ASCII character
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 (uppercase)
echo sprintf("%鑾u",$num1)."<br>"; // Unsigned decimal number (positive)
echo sprintf("%鑾u",$num2)."<br>"; // Unsigned decimal number (negative)
echo sprintf("%%f = %f",$num1)."<br>"; // Floating point number (affected by locale settings)
echo sprintf("%%F = %F",$num1)."<br>"; // Floating point number (not affected by locale 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 (uppercase)
echo sprintf("%%+d = %+d", $num1) . "<br>"; // Positive sign specifier
echo sprintf("%%+d = %+d", $num2) . "<br>"; // Negative sign specifier
?>

Run Instance

Example 4

Demonstration of string literals:

<?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>";
?>

Run Instance