sprintf(format, arg1, arg2, ...)其中,format为格式化字符串,arg1、arg2等为要格式化的参数。在format中,可以使用占位符来标识要替换的参数,常用的占位符有: - %s:字符串 - %d:整数 - %f:浮点数 例如,要将一个字符串和一个整数格式化成一个新的字符串,可以这样写:
php $str = 'Hello'; $num = 123; $newStr = sprintf('%s, world! The number is %d.', $str, $num); echo $newStr;输出结果为:
Hello, world! The number is 123.
$str = 'Hello';
$num = 123;
$newStr = sprintf('%s, world! The number is %d.', $str, $num);
echo $newStr;
另外,还可以使用printf函数直接将格式化后的字符串输出,例如:
php $str = 'Hello'; $num = 123; printf('%s, world! The number is %d.', $str, $num);输出结果同上。
$str = 'Hello';
$num = 123;
printf('%s, world! The number is %d.', $str, $num);
如果要将格式化后的字符串存储到变量中,可以使用sprintf函数。