我試圖在使用sprintf但不合格時在不同位置使用相同的值。sprintf多次使用相同的值
<?php
$score = 50;
$percent = 10;
$str = "Hello: You scored %s (%s%%). Your score is %2$s %%"; //Problem is here %2$s
echo sprintf($str,$score,$percent);
?>
我得到這個錯誤:Notice: Undefined variable: s in C:\web\apache\htdocs\sprintf.php on line 6 Warning: sprintf(): Too few arguments in C:\web\apache\htdocs\sprintf.php on line 8
但我需要這些雙引號。他們在那裏有一個目的。 – jmenezes
好的。我在'$'之前放了一個'\',現在沒問題。 – jmenezes
您也可以使用連接而不是插值來在字符串中執行變量替換。 – Barmar