Home >>PHP String Functions >PHP vsprintf() Function
PHP vsprintf() function is used to display array values as a formatted string. The given array elements will be inserted at the percent signs(%) in the given input string. It displays the array values as a formatted string according to its format and also accepts an array as argument in place of variables. The function returns formatted string while vprintf() outputs a formatted string.
Syntax:
vsprintf($format, $array);
Parameter | Description |
---|---|
format | This is a required parameter. This parameter defines the string and how to format the variables in it. |
array | This is a required parameter. This parameter defines an array with arguments to be inserted at the % signs in the format string. |
Here is an example of vsprintf() function in PHP:
<html> <body> <?php $name = "Jerry"; $age = "21"; $result = vsprintf("My name is %s and i'm %u years old.",array($name,$age)); echo $result; ?> </body> </html>
Here is an another example of vsprintf() function in PHP:
<html> <body> <?php $number = 955; $result = vsprintf("Price of this product is %1\$.2f",array($number)); echo $result; ?> </body> </html>