用法示例:
php > $a = 534 ;
php > printf("printf type is %.1f",$a);
printf type is 534.0
php > printf("printf type is %'u.1f",$a);
printf type is 534.0
php > printf("printf type is %'u6.1f",$a);
printf type is u534.0
php > printf("printf type is %'u10.1f",$a);
printf type is uuuuu534.0
php > printf("printf type is %'u-10.1f",$a);
printf type is 534.0uuuuu
php > printf("printf type is %b",$a);
printf type is 1000010110
php > printf("printf type is %o",$a);
printf type is 1026
php > printf("printf type is %s",$a);
printf type is 534
php > printf("printf type is %u",$a);
printf type is 534
php > printf("printf type is %x",$a);
printf type is 216
php > $a = 539;
php > printf("printf type is %x",$a);
printf type is 21b
当在类型转换代码中使用 printf() 函数时,你可以使用带序号的参数方式,这就意味着参数的顺序并不一定要与转换标记中的顺序相同。 例如:
printf ("you have %2\$.2f money , but shopping %1\$.2f RMB ", $total , $total_shopping ); 只要直接在 % 符号后添加参数的位置,并且以 $ 符号为结束 。 在这个例子中,2\$ 意味着用列表中的第二个参数替换。这个方法也可以在重复参数中使用。
php > $a = 539;
php > $b = 38;
php > printf("you have %2\$.2f money , but shopping %1\$.2f RMB! ", $a,$b);
you have 38.00 money , but shopping 539.00 RMB!
php >