<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>PHP-Scripts Blog &#187; Formatting</title>
	<link>http://www.php-scripts.com</link>
	<description>Writing about PHP scripting since 12/99. Learn something new every day.</description>
	<pubDate>Thu, 09 Oct 2008 14:17:07 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>
	<language>en</language>
			<item>
		<title>Formatting numbers with sprintf</title>
		<link>http://www.php-scripts.com/20070404/105/</link>
		<comments>http://www.php-scripts.com/20070404/105/#comments</comments>
		<pubDate>Thu, 05 Apr 2007 00:43:54 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Formatting]]></category>

		<category><![CDATA[Functions]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20070404/105/</guid>
		<description><![CDATA[In the game Keno you might have noticed that games begin at 001 and iterate to 999. Let&#8217;s look at some code to cycle properly and stay formatted in three digits using sprintf().

$game_number = 1;
echo &#8216;Game #&#8217; . sprintf&#40;&#8220;%03d&#8221;,$game_number&#41;; // Game #00x 
&#160;

Now let&#8217;s add some style magic to give the output a blackground with [...]]]></description>
			<content:encoded><![CDATA[<p>In the game Keno you might have noticed that games begin at 001 and iterate to 999. Let&#8217;s look at some code to cycle properly and stay formatted in three digits using sprintf().</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$game_number</span> = <span class="nu0">1</span>;<br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;Game #&#8217;</span> . <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8220;%03d&#8221;</span>,<span class="re0">$game_number</span><span class="br0">&#41;</span>; <span class="co1">// Game #00x </span><br />
&nbsp;</div>
</div>
<p>Now let&#8217;s add some style magic to give the output a blackground with red numbers:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$game_number</span> = <span class="nu0">1</span>;<br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;New game #&lt;font style=&quot;background-color: black;color: red&quot;&gt;&#8217;</span> . <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8220;%03d&#8221;</span>,<span class="re0">$game_number</span><span class="br0">&#41;</span> . <span class="st0">&#8216;&lt;/font&gt;&lt;br /&gt;&#8217;</span>;</div>
</div>
<p>Finally we need to create some program logic to test when to reset the game number to 1. An IF statement will suffice and we&#8217;ll also add some code to go to show the next game number.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$game_number</span> = <span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&#8216;gid&#8217;</span><span class="br0">&#93;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$game_number</span> &gt; <span class="nu0">999</span> or <span class="re0">$game_number</span> &lt; <span class="nu0">1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="re0">$game_number</span> = <span class="nu0">1</span>;<br />
<span class="br0">&#125;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;New game #&lt;font style=&quot;background-color: black;color: red&quot;&gt;&#8217;</span> . <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8220;%03d&#8221;</span>,<span class="re0">$game_number</span><span class="br0">&#41;</span> . <span class="st0">&#8216;&lt;br /&gt;&#8217;</span>;<br />
<span class="re0">$game_number</span>++;<br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8220;&lt;p&gt;&lt;a href=<span class="es0">\&#8221;</span>?gid=$game_number<span class="es0">\&#8221;</span>&gt;next game&lt;/a&gt;&lt;/p&gt;&#8221;</span>;</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20070404/105/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

