Exchange arguments sprintf and HEREDOC

I am trying to use sprintf on heredoc this way. This will not work. Any idea how to solve this?

$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
+4
source share
2 answers

Because HEREDOC acts like a double-quoted string, PHP tries to interpolate $sas a variable. Try NOWDOC instead

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

Or just avoid the dollar signs:

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

This is useful, for example, in PHP 5.2, where there is no nowdoc syntax.

+4
source

All Articles