Home >>PHP String Functions >PHP sprintf() Function
PHP sprintf() function is used to write a formatted string to a variable. It replaces the percent sign (%) with the variables passed as the argument and returns a formatted string as the output. This function is supported by PHP 4 and above versions.
Syntax:
sprintf($format,$arg1,$arg2,$arg++);
Parameter | Description |
---|---|
format | This is a required parameter. This parameter contains the string and how to format the variables in it. Some of the possible format values:
|
arg1 | This is a required parameter. This parameter contains the argument to be inserted at the first %-sign. |
arg2 | This is an optional parameter. This parameter contains the argument to be inserted at the second %-sign. |
arg++ | This is an optional parameter. This parameter contains the argument to be inserted at the upcoming %-sign. |
Here is an example of sprintf() function in PHP:
<html> <body> <?php $age = 20; $name = "Someone"; sprintf("%s is %u years old.",$name,$age); ?> </body> </html>
Here is an another example of sprintf() function in PHP:
<html> <body> <?php $price = 226; sprintf("%f",$price); ?> </body> </html>