无法正确理解PHP中的vprintf()

3 php string printf

我无法理解以下代码:

<?php
   $number = 123;
   vprintf("With 2 decimals: %1\$.2f
   <br>With no decimals: %1\$u",array($number));
?>
Run Code Online (Sandbox Code Playgroud)

浏览器输出:

With 2 decimals: 123.00 
With no decimals: 123
Run Code Online (Sandbox Code Playgroud)

但是这里只有一个元素在数组中,而它必须是两个参数.

还有什么意思 %1\$

ric*_*aan 8

这是一种指定要使用的参数的方法.%1$s表示第一个参数,%2$s第二个参数等.这是一种重用单个参数的方法,因此您不必在函数调用中多次提供相同的值:

$one = 'one';
$two = 'two';

printf('%s', $one); // 'one'
printf('%1$s', $one); // 'one'
printf('%s %s', $one, $two); // 'one two'
printf('%1$s %2$s', $one, $two); // 'one two'
printf('%2$s %1$s', $one, $two); // 'two one'

printf('%1$s %2$s %1$s', $one, $two); // 'one two one'
Run Code Online (Sandbox Code Playgroud)

在你的代码中,它被转义为a,\因为你的格式是双引号,如果没有转义美元符号,它会尝试解析变量$.2f$u(不存在).