PHP sprintf() 函数的应用(定义和用法) |
||||||||||
本文标签:PHP,sprintf 语法sprintf(format,arg1,arg2,arg++)
说明参数 format 是转换的格式,以百分比符号 ("%") 开始到转换字符结束 。下面的可能的 format 值: 复制代码 代码如下: ? <?php $str = "Hello"; $number = 123; $txt = sprintf("%s world. Day number %u",$str,$number); echo $txt; ?> 输出: Hello world. Day number 123 例子 2 复制代码 代码如下: <?php $number = 123; $txt = sprintf("%f",$number); echo $txt; ?> 输出: 123.000000 例子 3 复制代码 代码如下: <?php $number = 123; $txt = sprintf("With 2 decimals: %1\$.2f<br />With no decimals: %1\$u",$number); echo $txt; ?> 输出: 复制代码 代码如下: With 2 decimals: 123.00 With no decimals: 123 例子4 复制代码 代码如下: <?php $ctype_primary = strtolower(application); $ctype_secondary = strtolower(pdf); $mimetype = sprintf(%s/%s, $ctype_primary, $ctype_secondary); echo $mimetype; ?> 输出: 复制代码 代码如下: application/pdf |