PHP sprintf Function

What is sprintf Function and how it is used in string?

Explanation

This function is used to return a formatted string in PHP.

Syntax:


sprintf (format,arg1,arg2,arg++)

In the above syntax "format" specifies the format of the string to be output,in which user can include the following formatting characters " %% ,%b , %c, %d, %e, %u, %f, %F, %o, %s, %x, %X ", "arg1" is the argument inserted in the first % sign, "arg2" in the second % sign, "arg++" is an optional argument that can be inserted in third and fourth % signs.

Example :


<?php
$str = "HIOX INDIA";
$number = 365;
$val= sprintf("%s can host websites %u days",$str,$number);
echo $val
?>
Result :

HIOX INDIA can host websites 365 days

In the above example the strings are formatted and stored in a variable "$val".

PHP Topics


Ask Questions

Ask Question