In the previous article we introduced "Two ways to generate random numbers in php", this article will introduce thesprintf()
function . PHP may need to include multiple variables continuously when outputting a string. If you useecho
directly, you need to splice each variable and the string, which will be troublesome, so we can usesprintf()
function to solve this problem.
First let’s take a look at the syntax of thesprintf()
function:
sprintf ( string $format , $arg1 ) : string
$format: a string containing placeholders.
$arg1: The corresponding variable in the string.
Code example:
"; echo sprintf("变量a八进制为:%o;",$a); echo "
"; echo sprintf("变量a十进制为:%d;",$a); echo "
"; echo sprintf("变量a十六进制为:%o;",$a); echo "
"; echo sprintf("变量b字符串为:%s",$b); echo "
";
输出:变量a二进制为:1010; 变量a八进制为:12; 变量a十进制为:10; 变量a十六进制为:12; 变量b字符串为:php.cn
Recommended:《2021 PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of Analysis of sprintf() function in php (with code example). For more information, please follow other related articles on the PHP Chinese website!