PHP String sprintf() functionThe 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.SyntaxThe 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. Parameterformat (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
Note: The c type specifier ignores width and padding.Type Handling
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
Values ReturnThe sprint() function returns the formatted string. Supported VersionThis function is supported by PHP 4 and above versions. ExamplesBelow 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 PHPThe 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. Now, let's see the working of printf() function. Output: Next TopicPHP String Functions |