sprintf()方法的常用使用方法

1. 取得一个数字,位数不足补零
<?php
$year = sprintf("%04d", 2011);
$month = sprintf("%02d", 1);
$day = sprintf("%02d", 1);
echo $year.'-'.$month.'-'.$day; //2011-01-01
?>
2. 参数交换,先输出第2个参数,在输出第1个参数
<?php
$format = 'The %2$s contains %1$d monkeys';
printf($format, 10, 100); //The 100 contains 10 monkeys
?>
3. 参数交换:重复参数占位符
<?php
$format = 'The %2$s contains %1$d monkeys.That\'s a nice %2$s full of %1$d monkeys.';
printf($format, 10, 100); //The 100 contains 10 monkeys.That\'s a nice 100 full of 10 monkeys.
?>
原文地址:https://www.cnblogs.com/phpfans/p/2242487.html