English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP sprintf() Function Usage and Example

   PHP String Function Manual

    The sprintf() function is used to return a formatted string.

Syntax

string sprintf ( string $format [, mixed $args [, mixed $... ]])

Definition and Usage

It is used to format strings, and write the formatted string into a variable.

arg1, arg2,++ The parameter will be inserted at the percentage (%) symbol in the main string. The function is executed step by step. Insert arg at the first % symbol.1Insert arg at the second % symbol.2and so on.

Note:If the % symbol is more than the arg parameter, you must use placeholders. Placeholders are inserted after the % symbol, consisting of numbers and "\$". See examples. 2.

Tip:Related functions:printf(),vprintf(),vsprintf(),fprintf() andvfprintf()

Return value

It returns the formatted string

Parameter

NumberParameter and description
1

format

Required. It specifies the string and how to format the variable within it

Possible format values include:

  • %% - Returns a percentage sign %%

  • %b - Binary number

  • %c - Character corresponding to ASCII value

  • %d - Decimal number with a sign (negative number, 0, positive number)

  • %e - Use lowercase scientific notation (for example 1.2e+2

  • %E - Use uppercase scientific notation (for example 1.2E+2

  • 㩵n - Decimal number without a sign (greater than or equal to 0)

  • %f - Floating-point number (local settings)

  • %F - Floating-point number (non-local settings)

  • %g - Short %e and %f

  • %G - Short %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 (for example %.2f):

  • + (add + or - 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 the fill, the default is a space. It must be used with the width specifier. For example: %'x20s(use "x" as the fill)

  • - (left-adjust 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 specified above and cannot be mixed up.

2

arg1

Required. It specifies the first parameter to be inserted into the formatted string

3

arg2

Optional. It specifies the second parameter to be inserted into the formatted string

...

arg++

Optional. Specifies the parameter to be inserted at the third, fourth, fifth, and so on, %% symbols in the format string.

Online example

Try the following example, replacing the symbol %f with a variable passed as a parameter:

<?php
   //Replace the symbol %f with a variable passed as a parameter
   $number = 123;
   $txt = sprintf("%f", $number);
   
   echo $txt;
?>
Test and see‹/›

The result should be as follows

123.000000

Demonstration examples of all possible format values of the parameter format:

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // ASCII character 50 is 2
 
// Note: The format value "%%" returns a percentage sign
echo sprintf("%%b = %%b", $num1)."<br>"; // Binary number
echo sprintf("%%c = %%c", $char)."<br>"; // ASCII character
echo sprintf("%%d = %d",$num1)."<br>"; // echo sprintf("%%d = %d",$num
echo sprintf("%%d = %d",$num2)."<br>"; // echo sprintf("%%d = %d",$num
Signed decimal number1)."<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 (considering local settings)
echo sprintf("%%F = %F",$num1)."<br>"; // Floating-point number (not considering 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 (uppercase)
echo sprintf("%%+d = %+d",$num1)."<br>"; // Sign specifier (positive)
echo sprintf("%%+d = %+d",$num2)."<br>"; // Sign specifier (negative)
?>
Test and see‹/›

The result should be as follows

%b = 111010110111100110100010101
%c = 2
%d = 123456789
%d = -123456789
%e = 1.234568e+8
 1.234568E+8
頻, 123456789
頻, 18446744073586094827
%f = 123456789.000000
%F = 123456789.000000
%g = 1.23457e+8
%G = 1.23457E+8
%o = 726746425
%s = 123456789
%x = 75bcd15
%X = 75BCD15
%+d = +123456789
%+d = -123456789

Demonstration examples of string specifiers:

<?php
$str1 = "Hello";
$str2 = "Hello PHP!";
 
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>";
?>
Test and see ‹/›

Output Result:

[Hello]
[   Hello]
[Hello   ]
[000Hello]
[***Hello]
[Hello PH]

PHP String Function Manual