Home >>PHP String Functions >PHP printf() Function
PHP string printf() function is used to get the output as a formatted string. We can pass multiple parameters in this function. The arguments will be inserted at percent (%) signs one by one in the respected order. Args1 will be inserted at first percent(%) sign, args2 will be inserted at second percent (%) sign and so on.
Syntax:
printf($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 printf() function in PHP:
<html> <body> <?php $age = 18; $name = "Someone"; printf("%s is %u years old.",$name,$age); ?> </body> </html>
Here is an another example of printf() function in PHP:
<html> <body> <?php $price = 736; printf("%f",$price); ?> </body> </html>