note 97575 added to function.sprintf

From: Date: Mon, 26 Apr 2010 20:50:46 +0000
Subject: note 97575 added to function.sprintf
Groups: php.notes 
Request: Send a blank email to php-notes+get-168679@lists.php.net to get a copy of this message
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. ---- Server IP: 69.147.83.197 Probable Submitter: 209.203.97.50 ---- Manual Page -- http://www.php.net/manual/en/function.sprintf.php Edit -- https://master.php.net/note/edit/97575 Del: integrated -- https://master.php.net/note/delete/97575/integrated Del: useless -- https://master.php.net/note/delete/97575/useless Del: bad code -- https://master.php.net/note/delete/97575/bad+code Del: spam -- https://master.php.net/note/delete/97575/spam Del: non-english -- https://master.php.net/note/delete/97575/non-english Del: in docs -- https://master.php.net/note/delete/97575/in+docs Del: other reasons-- https://master.php.net/note/delete/97575 Reject -- https://master.php.net/note/reject/97575 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168679) next »