我正试图以这种方式在heredoc上使用sprintf。它不会工作。知道如何解决这个问题吗?
$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 :(得分:6)
因为HEREDOC就像一个双引号字符串,PHP试图将$s
插入为变量。请尝试使用NOWDOC
$i = <<<'EOD'
This is your invoice for %1$s %1$s %1$s %1$s
EOD;
答案 1 :(得分:4)
或者只是逃避美元符号:
$i = <<<EOD
This is your invoice for %1\$s %1\$s %1\$s %1\$s
EOD;
这很有用,例如在PHP 5.2中没有nowdoc语法。