(编辑:jimmy 日期: 2025/12/24 浏览:2)
PHP sprintf() 函数
把百分号(%)符号替换成一个作为参数进行传递的变量:
<"Shanghai";
$txt = sprintf("There are %u million cars in %s.",$number,$str);
echo $txt;
"\$" 组成。
语法
参数
描述
format
必需。规定字符串以及如何格式化其中的变量。
可能的格式值:
附加的格式值。必需放置在 % 和字母之间(例如 %.2f):
注释:如果使用多个上述的格式值,它们必须按照以上顺序使用。
arg1 必需。规定插到 format 字符串中第一个 % 符号处的参数。 arg2 可选。规定插到 format 字符串中第二个 % 符号处的参数。 arg++ 可选。规定插到 format 字符串中第三、四等 % 符号处的参数。PHP printf() 函数
输出格式化的字符串:
<"北京";
printf("在%s有 %u 百万辆自行车。",$str,$number);
"htmlcode">
printf(format,arg1,arg2,arg++)
参数
描述
format
必需。规定字符串以及如何格式化其中的变量。
可能的格式值:
附加的格式值。必需放置在 % 和字母之间(例如 %.2f):
注释:如果使用多个上述的格式值,它们必须按照上面的顺序进行使用,不能打乱。
arg1 必需。规定插到 format 字符串中第一个 % 符号处的参数。 arg2 必需。规定插到 format 字符串中第二个 % 符号处的参数。 arg++ 可选。规定插到 format 字符串中第三、四等等 % 符号处的参数。下面是一个示例:四舍五入保留小数点后两位
<"%0.2f",$num1)."<br />"; //输出 21.00
$num2 = 16.3287;
echo sprintf("%0.2f",$num2)."<br />"; //输出 16.33
$num3 = 32.12329;
echo sprintf("%0.2f",$num3)."<br />"; //输出 32.12
"color: #0000ff">printf与sprintf的区别
1. printf函数:
int printf ( string format [, mixed args [, mixed ...]] )
Produces output according to format , which is described in the documentation for sprintf() .
Returns the length of the outputted string.
把文字格式化以后输出,如:
$name="hunte";
$age=25;
printf("my name is %s, age %d", $name, $age);
2. sprintf函数:
string sprintf ( string format [, mixed args [, mixed ...]] )
Returns a string produced according to the formatting string format .
跟printf相似,但不打印,而是返回格式化后的文字,其他的与printf一样。
3. print函数:
是函数,可以返回一个值,只能有一个参数。
int print ( string arg )
Outputs arg . Returns 1 , always.