2013-10-31 69 views
4

我試圖在heredoc上使用sprintf這種方式。它不會工作。任何想法如何解決這個問題?sprintf參數交換和HEREDOC

$i = <<<EOD 
This is your invoice for %1$s %1$s %1$s %1$s 
EOD; 

$year = '2013'; 

$i = sprintf($i,$year); 

echo $i; 

Notice: Undefined variable: s in 
+0

你期望的結果是什麼? – srain

回答

6

因爲HEREDOC就像一個雙引號字符串,PHP試圖插值$s作爲變量。嘗試NOWDOC代替

$i = <<<'EOD' 
This is your invoice for %1$s %1$s %1$s %1$s 
EOD; 
+0

這工作。非常感謝。 – jmenezes

4

或者只是逃避的美元符號:

$i = <<<EOD 
This is your invoice for %1\$s %1\$s %1\$s %1\$s 
EOD; 

這是非常有用例如在PHP 5.2中沒有nowdoc語法。