note 97575 deleted from function.sprintf by philip
| From: | philip@php.net | Date: | Fri, 11 Jun 2010 05:31:31 +0000 |
| Subject: | note 97575 deleted from function.sprintf by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170045@lists.php.net to get a copy of this message | ||
Note Submitter: ayemossum at gmail dot com
----
It was mentioned that reordering does not work when creating your format string with heredoc format.
This is actually incorrect.
The following will not work
<?php
$format = <<<EOD
The %2$s contains %1$d monkeys
EOD;
echo sprintf($format, $num, $location );
?>
This is not a problem with heredoc. PHP is trying to substitute the variables $s and $d.
The same problem arises if you write it thus
<?php
$format = "The %2$s contains %1$d monkeys";
echo sprintf($format, $num, $location );
?>
For this to work (as with anytime you want to have a $ character in a double-quoted or heredoc
string), you must escape the $ like so:
<?php
$format = <<<EOD
The %2\$s contains %1\$d monkeys
EOD;
echo sprintf($format, $num, $location );
$format = "The %2\$s contains %1\$d monkeys";
echo sprintf($format, $num, $location );
?>
You may also use the "nowdoc" syntax instead of heredoc:
<?php
$format = <<<'EOD'
The %2$s contains %1$d monkeys
EOD;
echo sprintf($format, $num, $location );
?>
This works the same as if you were to use single-quotes.