PHP String sprintf() function

The sprintf() is an in-built function of PHP which writes a formatted string to a variable. It returns a formatted string. PHP 4 and above versions support sprintf() function.

The sprintf() function is similar to the printf() function, but the only difference between both of them is that sprint() saves the output into a string instead of displaying the formatted message on browser like printf() function.

Note: The sprintf() works with echo. The formatted string returns by the sprintf() function is printed by echo on the browser whereas printf() directly put the output on browser.

Syntax

The syntax of the sprintf() is given below:

Here the arg1, arg2, arg3, etc. are the parameters of sprintf(). These parameters will be inserted in the main string with percent (%) symbols. At each % sign, arguments will insert one-by-one.

Parameter

format (mandatory)

This parameter is a mandatory parameter which specifies the string and describes how to format the variable in it. In this, only simple characters excluding % are copied directly to the result string, but the character with % sign fetches its own parameters. The possible format values are:

Specifiers

ParameterDescription
%%It returns a percent (%) sign.
%bThe argument is presented as a binary number.
%cThe parameter is treated as an integer and represented as the character with that ASCII.
%dThe parameter is treated as a positive integer and represented as a decimal number.
%eScientific notation with lowercase, e.g., 1.2e+2. The precision specifier specifies that the number of digits after the decimal point to be printed.
%ESimilar to the e specifier but the scientific notation with uppercase, e.g., 1.2E+2.
%uThe parameter is treated as an integer and represented as an unsigned integer.
%fFloating-point number (locale aware)
%FFloating-point number (non-locale aware)
%gIt is a general format.
%GSimilar to g specifier but it uses E and F.
%oRepresented as an octal number.
%sThe argument is treated as well as presented as a string.
%xIt is represented as a hexadecimal number with lowercase letters.
%XIt is also represented as a hexadecimal number but with uppercase letters.

Note: The c type specifier ignores width and padding.

Type Handling

TypeSpecifiers
strings
integerd, u, c, o, x, X, b
doubleG, g, E, e, F, f

Some additional format values also exist which are placed between the percent sign (%) and the letter. (For example: %.2f)

These additional format values are listed below:

Flags

FlagDescription
-Left-justify within the given field and right justification is by default
+Prefixed both +ive and -ive sign in front of numbers. By default, a negative sign is placed before the negative number only.
(space)It is the default, and it pads the results with space.
0Only left-pads number with zero and with s specifiers it can also right-pads with zero.
'(char)It pads the result with the character.

Values Return

The sprint() function returns the formatted string.

Supported Version

This function is supported by PHP 4 and above versions.

Examples

Below some examples are given to learn the practical implementation of the sprintf() function.

Example 1: Simple Example

Output:

It is the basic example of PHP String function.

Example 2: Variable declaration

Output:

This is the 1st example of the sprintf function.
This function works with echo.

Example 3: Argument swapping

Let's see the complete example3 to understand the concept of argument swapping.

Output:

There are 54 students in PHP training batch in the 2018 year.

Here, if we swap the order of the placeholder in format string then it will create a problem for us. It does not match with the order of argument in the code. Hence, placeholder does not match with the order of argument. Let's see the below code-

Output:

There are 0 students in 54 batch in the 2018 year

So, if we want to leave the code as it is and want to correctly indicate that which arguments the placeholder refers to, then write it as below given code-

Output:

Now, the output is same as the original output.

There are 54 students in PHP training batch in the 2018 year

Note: In that case, we need to define the place of the argument to print the correct output of the code.

Example 4: Specifying padding character

Output:

Now, the output of the above code for padding character would be like-

....1234 
00001234

Example 5: Specifying padding character

Output:

Now, the output of the above code for padding character would be like-

3.26e+9
****Hi 
Hi**** 
125.235000 
125.23 
125 
125.23500000

Difference between sprintf() and printf() function in PHP

The common difference between sprintf() and printf() function is that sprintf() function display the text with the help of echo, whereas printf() function does not need the echo to display the text. We will show this difference with the help of the example.

Example

Output:

Here, we can see that text which is stored by the variable $str1 is not printed on the browser directly by sprintf() function, so we use echo to display the string stored by str2 variable.

PHP String sprintf() function

Now, let's see the working of printf() function.

Output:

PHP String sprintf() function




Latest Courses