Easy Tutorial
❮ Func Mysqli Real Escape String Func Mysqli Connect Errno ❯

PHP sprintf() Function

PHP String Reference Manual

Example

Replace the percent (%) sign with a variable passed as an argument:

<?php
$number = 9;
$str = "tutorialpro";
$txt = sprintf("%s has %u million visitors per day!", $str, $number);
echo $txt;
?>

Execution output:

tutorialpro has 9 million visitors per day!

Definition and Usage

The sprintf() function writes a formatted string to a variable.

The arg1, arg2, ++ parameters will be inserted at the percent (%) sign in the main string. The function is executed step by step. At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, and so on.

Note: If there are more % signs than arg parameters, you must use placeholders. Placeholders are inserted after the % sign and consist of a number and a "$". See Example 2.

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


Syntax

Parameter Description
format Required. Specifies the string and how to format the variables in it. Possible format values: %% - Returns a percent sign %<br> %b - Binary number<br> %c - Character corresponding to the ASCII value<br> %d - Signed decimal number (negative, zero, positive)<br> %e - Lowercase scientific notation (e.g., 1.2e+2)<br> %E - Uppercase scientific notation (e.g., 1.2E+2)<br> %u - Unsigned decimal number (greater than or equal to 0)<br> %f - Floating point number (locale aware)<br> %F - Floating point number (non-locale aware)<br> %g - Shorter of %e and %f<br> %G - Shorter of %E and %f<br> %o - Octal number<br> %s - String<br> %x - Hexadecimal number (lowercase letters)<br> %X - Hexadecimal number (uppercase letters) Additional format values. Must be placed between % and the letter (e.g., %.2f): + (Adds a + or - sign before the number. By default, only negative numbers are marked, positive numbers are not marked)<br> ' (Specifies what to use as padding. Default is space. It must be used with a width specifier. For example: %'x20s (uses "x" as padding))<br> - (Left-justifies the variable value)<br> [0-9] (Specifies the minimum width of the variable value)<br> .[0-9] (Specifies the number of decimal places or the maximum string length) Note: If multiple format values are used, they must be used in the order shown above, without being scrambled.
arg1 Required. Specifies the parameter to insert at the first % sign in the format string.
arg2 Optional. Specifies the parameter to insert at the second % sign in the format string.
arg++ Optional. Specifies the parameter to insert at the third, fourth, etc., % sign in the format string.

Technical Details

Return Value: Returns the formatted string.
PHP Version: 4+
--- ---

More Examples

Example 1

Using the format value %f:

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

Example 2

Using placeholders:

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

Example 3

Demonstration of all possible format values:

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // ASCII character 50 is 2

// Note: The format value "%%" 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 = %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 (locale aware)
echo sprintf("%%F = %F", $num1) . "<br>"; // Floating point number (non-locale aware)
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)
?>

Example 4

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>";
?>
❮ Func Mysqli Real Escape String Func Mysqli Connect Errno ❯