Javatpoint Logo
Javatpoint Logo

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

Parameter Description
%% It returns a percent (%) sign.
%b The argument is presented as a binary number.
%c The parameter is treated as an integer and represented as the character with that ASCII.
%d The parameter is treated as a positive integer and represented as a decimal number.
%e Scientific notation with lowercase, e.g., 1.2e+2. The precision specifier specifies that the number of digits after the decimal point to be printed.
%E Similar to the e specifier but the scientific notation with uppercase, e.g., 1.2E+2.
%u The parameter is treated as an integer and represented as an unsigned integer.
%f Floating-point number (locale aware)
%F Floating-point number (non-locale aware)
%g It is a general format.
%G Similar to g specifier but it uses E and F.
%o Represented as an octal number.
%s The argument is treated as well as presented as a string.
%x It is represented as a hexadecimal number with lowercase letters.
%X It is also represented as a hexadecimal number but with uppercase letters.

Note: The c type specifier ignores width and padding.

Type Handling

Type Specifiers
string s
integer d, u, c, o, x, X, b
double G, 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

Flag Description
- 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.
0 Only 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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA