note 102709 added to language.types.string

From: Date: Tue, 01 Mar 2011 20:15:57 +0000
Subject: note 102709 added to language.types.string
Groups: php.notes 
Request: Send a blank email to php-notes+get-177460@lists.php.net to get a copy of this message
I recently discovered the joys of using heredoc with sprintf and positions. Useful if you want some code to iterate, you can repeat placeholders. <?php function getNumber($num = 0) { $foo = rand(1,20); return ($foo + $num); } function getString() { $foo = array("California","Oregon","Washington"); shuffle($foo); return $foo[0]; } function getDiv() { $num = getNumber(); $div = sprintf( "<div>%s</div>", getNumber(rand(-5,5)) ); return $div; } $string = <<<THESTRING I like the state of %1\$s <br /> I picked: %2\$d as a number, <br /> I also picked %2\$d as a number again <br /> %3\$s<br /> %3\$s<br /> %3\$s<br /> %3\$s<br /> %3\$s<br /> THESTRING; $returnText = sprintf( $string, getString(),getNumber(),getDiv() ); echo $returnText; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 198.67.17.9 ---- Manual Page -- http://www.php.net/manual/en/language.types.string.php Edit -- https://master.php.net/note/edit/102709 Del: integrated -- https://master.php.net/note/delete/102709/integrated Del: useless -- https://master.php.net/note/delete/102709/useless Del: bad code -- https://master.php.net/note/delete/102709/bad+code Del: spam -- https://master.php.net/note/delete/102709/spam Del: non-english -- https://master.php.net/note/delete/102709/non-english Del: in docs -- https://master.php.net/note/delete/102709/in+docs Del: other reasons-- https://master.php.net/note/delete/102709 Reject -- https://master.php.net/note/reject/102709 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177460) next »