PHP sprintf() Function

PHP sprintf() function lets you return a string in your chosen format. For example, you want to add a plus or minus sign in front of a number like +100 or -100. Or, you want to add two zeros after decimal point like 200.00. You can do all these with this function.

Syntax:

sprintf(format, argument1, argument2, …)

Parameters:

The function has 2 required parameters and unlimited optional parameter-

format (Required): In this parameter, mention the string that you want to format. It contains zero or more placeholders. Each placeholder defines a rule (or conversion specification) to format the value that will be inserted in it by an argument.

argument1 (Required): argument1 is the value that is to be formatted by the rules (or conversion specification) set in the placeholders. argument1 is formatted by the 1st placeholder’s rule.

argument2… (Optional): argument2… are the values that are to be formatted by the rules (or conversion specification) set in the placeholders. Argument2 is formatted by the 2nd placeholder’s rule, argument3 is formatted by the 3rd placeholder’s rule and so on.

A conversion specification has the following syntax-

%[argnum$][flags][width][.precision]specifier.

  • %: Each conversion specification starts with percentage sign, %.
  • argnum: it is an integer followed by the dollar sign $ that tells which argument number to be used. Ex. $1 means first argument, $2 means second argument. So, you can change the regular order of the arguments that are to be used in the placeholders. Or you can repeat arguments if you wish.
  • flags: A flag consists of the following characters-
    It left-justify within the given field width. See Example 1 (line 2). Right-justification is the default.
    +It adds plus sign (+) in front of a positive number. See example 1 (line 3).
    (space)It pads the result with spaces. See example 1 (line 4).
    0It adds zero at the left of a number. If you use s specifier, it adds 0 at right too. See example 1 (line 4)
    ‘(char)It pads the result with the character (char). See example 1 (line 5)
  • width: After conversion, how many characters (minimum) should be produced is indicated by the width which is an integer number. 1 due.
  • .precision: It is a combination of a period followed by a integer like (.2) or a period followed by * (.*). It’s meaning depends on the specifier to be used next.  it Is very useful for floating point formatting. flags: A flag consists of the following characters-
    For e, E, f and F specifiers:When it is used with these specifiers, it tells how many digits to be displayed after decimal number (6 is the default).
    For g, G, h and H specifiers:When it is used with these specifiers, it tells how many significant digits to be displayed.
    For s specifierWhen it is used with s specifiers, it tells after how many characters the string will be cutoff.
  • specifier: This is a single character that indicates by which type the argument is treated and presented.
    %It displays a percent sign %.
    bIt treats a parameter (argument1, argument2, …) as a integer and displays it as binary number.
    cIt treats a parameter (argument1, argument2, …) as a integer and displays it as an ASCII character equivalent value.
    dIt treats a parameter (argument1, argument2, …) as a integer and displays it as a signed decimal number.
    eIt treats a parameter (argument1, argument2, …) as a scientific notation and displays it in lowercase.
    EIt treats a parameter (argument1, argument2, …) as a scientific notation and displays it in uppercase.
    fIt treats a parameter (argument1, argument2, …) as a float and displays it as a floating-point number (locale setting dependent)
    FIt treats a parameter (argument1, argument2, …) as a float and displays it as a floating-point number (not locale setting dependent)
    gGeneral format. It uses e and f
    GGeneral format like g, but, It uses E and f.
    hGeneral format like g, but, It uses F. Available at PHP 8.0.0.
    HGeneral format like g, but, It uses E and F. Available at PHP 8.0.0.
    oIt treats a parameter (argument1, argument2, …) as a integer and displays it as an octal number.
    sIt treats a parameter (argument1, argument2, …) as a string and displays it as a string.
    uIt treats a parameter (argument1, argument2, …) as a integer and displays it as an unsigned decimal number.
    xIt treats a parameter (argument1, argument2, …) as a integer and displays it as a lowercase hexadecimal number.
    XIt treats a parameter (argument1, argument2, …) as a integer and displays it as a uppercase hexadecimal number.

Return Values:

It returns the formatted string that you get from the format parameter.

Examples:

Example 1: Changing Arguments order.

<?php
// Changing Arguments order.
echo sprintf("%1\$d and %2\$d make %3\$s", 2, 3, "Six"); // Output: 2 and 3 make Six
?>

Example 2: Usages of Different flag and width

<pre>
<?php
// Left-justifies the string value with spaces
echo sprintf("[%-10s]", "PHP"); // Output: [PHP       ]
// Right-justifies the string with spaces.
echo sprintf("[%+10s]","PHP"); // Output: [PHP       ]
?>
</pre>
<?php
// Adding + sign in front of a number
echo sprintf("%+d", 100);   // Output: +100
// Left-pads a number with zeros.
echo sprintf("%05d", 123); // Output: 00123
// Right-pads a string with zeros.
echo sprintf("%05s", "PHP"); // Output: 00123 // NOT WORKING
// Pads a string with @ character
echo sprintf("%'@5s", "PHP"); // Output: @@PHP
// Default precision of 6 digits decimal number after decimal point.
echo sprintf( "%f", 34.567 );      // Output: 34.567000
// Displaying a floating number with 2 digits after decimal number.
echo sprintf( "%.2f", 34.567 );  // Output: 34.57
// Precision point with s specifier
echo sprintf( "%.13s", "PHP Functions abc" );  // Output: PHP Functions
// Displaying % sign
echo sprintf( "printf is top 5%% used functions in PHP.");  // Output: printf is top 5% used functions in PHP.
?

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Difference between sprint() and printf() functions:

Both functions generate same output. PHP printf() function directly prints the output to the browser, while PHP sprint() function returns the output and you can store it in a variable and print it with echo.

Summary: PHP sprintf() Function

sprintf() is a another very useful and frequently used built-in PHP string function.

Reference:

https://www.php.net/manual/en/function.sprintf.php