<?xml version="1.0" encoding="UTF-8"?>
<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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP Reference Book Blog &#187; Function</title>
	<atom:link href="http://www.phpreferencebook.com/tag/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpreferencebook.com</link>
	<description>PHP Reference: Beginner to Intermediate PHP5</description>
	<lastBuildDate>Wed, 25 Aug 2010 12:58:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Sprintf() Function Sample</title>
		<link>http://www.phpreferencebook.com/samples/sprintf-function/</link>
		<comments>http://www.phpreferencebook.com/samples/sprintf-function/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 06:42:49 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Samples]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[sample]]></category>
		<category><![CDATA[simple]]></category>
		<category><![CDATA[sprintf]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=14</guid>
		<description><![CDATA[sprintf(formatting, inputs [, ...inputs...] Accepts multiple inputs to be used when specified in formatting formatting – $string, specific formatting string, explained below inputs – $scalar(s) to be formatted Returns a formatted string formatting, using the inputs to dynamically input their values into the formatted string using a preset set of rules, specified below. The following [...]]]></description>
			<content:encoded><![CDATA[<p class="western" style="background: #000000 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"><span style="color: #ffffff; font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US">sprintf(</span></span><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em>formatting</em></span></span><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">, </span></span></span><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em>inputs</em></span></span><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"> [, ...</span></span></span><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em>inputs</em></span></span><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">...]</span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em>Accepts multiple </em></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em><span>inputs to be used when specified in formatting</span></em></span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em>formatting</em></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"> – </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>$string</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">, specific formatting string, explained below<br />
</span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em>inputs </em></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">– </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>$scalar</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>(s) to be formatted</span></span></span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>Returns a formatted string </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em><span>formatting</span></em></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>, using the </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em><span>inputs</span></em></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> to dynamically input their values into the formatted string using a preset set of rules, specified below.</span></span></span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>The following is the available nomenclature for the </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em><span>formatting</span></em></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> input.</span></span></span></span></span></span></p>
<hr />
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>Every time an input is expected to be used and evaluated as part of the formatted string, it is preceded by a percent sign ( % ), followed by the specifiers/rules:<br />
</span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em><strong>Note:</strong></em></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em><span> All specifiers, excluding the </span></em></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em><strong>type</strong></em></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em><span> specifier, are optional.</span></em></span></span></span></span></p>
<ul>
<li>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>A </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>sign</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> specifier. Placing a plus sign ( + ) forces negative AND positive 	signs to be visible (only negative values are specified by default).</span></span></span></span></span></span></p>
</li>
<li>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>A </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>padding</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> specifier. The default is a space, and does not need to be 	specified. A zero ( 0 ) can be used as well without any secondary 	notation. If any other character is to be used, it should be 	preceded with a single quote ( &#8216; ).</span></span></span></span></span></span></p>
</li>
<li>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>An </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>alignment</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> specifier. The default is right-justified (thus padding is placed on 	the left of the string). Placing a dash/subtract ( &#8211; ) will set it 	to left-justified.</span></span></span></span></span></span></p>
</li>
<li>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>A </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>width</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> specifier. This integer determines the minimum length in characters 	the output should be. When combined with padding, the specified 	width minus the input&#8217;s length determines the number of padded 	characters that will be added. </span></span></span></span></span></span></p>
</li>
<li>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>A </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>precision </strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>specifier. 	A period ( . ) followed by an integer, sets the number of decimal 	places that should be output for a float. If used on a string, it 	sets a maximum character limit for the output.</span></span></span></span></span></span></p>
</li>
<li>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span>A </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>type</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> specifier:</span></span></span></span></span></span></p>
<ul>
<li>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><em><span>%</span></em></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> &#8211; a literal percent sign, thus would be written %% to display a 		percent sign in the formatting string</span></span></span></span></span></span></p>
</li>
<li>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>b</em></span><span style="font-family: Palatino Linotype,serif;"> – the input should be an integer, a binary number is the output. </span></span></p>
</li>
<li>
<p class="western"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>c</em></span><span style="font-family: Palatino Linotype,serif;"> &#8211; the input should be an integer between 0-255, representing the 		ASCII byte-value. The character represented is output.</span></span></p>
</li>
<li>
<p class="western"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>d</em></span><span style="font-family: Palatino Linotype,serif;"> – the input should be an integer.</span></span></p>
</li>
<li>
<p class="western"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>e</em></span><span style="font-family: Palatino Linotype,serif;"> – the input is scientific notation.</span></span></p>
</li>
<li>
<p class="western"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>u</em></span><span style="font-family: Palatino Linotype,serif;"> – the input is an unsigned decimal number. </span></span></p>
</li>
<li>
<p class="western"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>f</em></span><span style="font-family: Palatino Linotype,serif;"> – the input is a float (locale aware). </span></span></p>
</li>
<li>
<p class="western"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>F</em></span><span style="font-family: Palatino Linotype,serif;"> &#8211; the input is a float (not locale aware).</span></span></p>
</li>
<li>
<p class="western"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>o</em></span><span style="font-family: Palatino Linotype,serif;"> – the input is an integer, an octal number is the output. </span></span></p>
</li>
<li>
<p class="western"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>s</em></span><span style="font-family: Palatino Linotype,serif;"> – the input is a string.</span></span></p>
</li>
<li>
<p class="western"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>x</em></span><span style="font-family: Palatino Linotype,serif;"> &#8211; the input is an integer, a hexadecimal number is the output  		(with lowercase letters). </span></span></p>
</li>
<li>
<p class="western"><span style="font-size: medium;"><span style="font-family: Palatino Linotype,serif;"><em>X</em></span><span style="font-family: Palatino Linotype,serif;"> &#8211; the input is an integer, a hexadecimal number is the output  		(with uppercase letters). </span></span></p>
</li>
</ul>
</li>
</ul>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span style="font-size: medium;"><em>Examples:</em></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span style="font-size: medium;">Basic substitution, no optional specifiers</span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">$string = &#8216;cat&#8217;;<br />
$integer = 10;<br />
echo sprintf(&#8220;I have %d %s(s)&#8221;, $integer, $string);</span></span></span></p>
<pre style="background: #c0c0c0 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">I have 10 cat(s)</span></span></span></pre>
<p class="western" style="margin-bottom: 0.07in; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span style="font-size: medium;">Basic substitution, type specification automatic adjustments</span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">$string = &#8216;cat&#8217;;<br />
$string2 = &#8217;10 blah&#8217;;<br />
echo sprintf(&#8220;I have %d %s(s)&#8221;, $string2, $string);</span></span></span></p>
<pre style="background: #c0c0c0 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">I have 10 cat(s)</span></span></span></pre>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">Using the </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>sign</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"> specifier</span></span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">$string = &#8216;cat&#8217;;<br />
$integer = &#8217;10&#8242;;<br />
echo sprintf(&#8220;Dagger has a %+d against %ss&#8221;, $integer, $string);</span></span></span></p>
<pre style="background: #c0c0c0 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">Dagger has a +10 against cats</span></span></span></pre>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">Using </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>padding</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"> and </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>width</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"> specifiers (default </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>padding</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"> specifier of a space)</span></span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">$string = &#8216;cat&#8217;; // length, 3 characters<br />
echo &#8216;&lt;pre&gt;&#8217;; // HTML Required to display the formating properly<br />
echo sprintf(&#8220;3 spaces added: |%6s&#8221;, $string);<br />
// Used padding of 6 characters, 6 – 3 = 3 spaces padded</span></span></span></p>
<pre style="background: #c0c0c0 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">Pad from line 3 spaces: |   cat</span></span></span></pre>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">Using </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>padding</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"> and </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>width</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"> using a zero ( 0 ) for padding</span></span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">$month = 12;<br />
$day = 1;<br />
$year = 1980;<br />
echo sprintf (&#8221; Date: %02d/%02d/%04d.&#8221;, $month, $day, $year);<br />
$year = 80;<br />
echo sprintf (&#8221; Date: %02d/%02d/%04d.&#8221;, $month, $day, $year);</span></span></span></p>
<pre style="background: #c0c0c0 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">Date: 12/01/1980. Date: 12/01/0080.</span></span></span></pre>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">Using </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>padding</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> and </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>width</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> using a custom character, the asterisk ( * )</span></span></span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">$endofpassword = &#8216;word&#8217;;<br />
$output = sprintf(&#8220;Your password: %&#8217;*8s&#8221;, $endofpassword);<br />
echo $output;</span></span></span></p>
<pre style="background: #c0c0c0 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">Your password: ****word</span></span></span></pre>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">Using </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>padding</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">, </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>alignment</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"> (left), and </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>width</strong></span></span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">$endofpassword = &#8216;word&#8217;;<br />
$output = sprintf(&#8220;Your password: %&#8217;*-8s&#8221;, $endofpassword);<br />
echo $output;</span></span></span></p>
<pre style="background: #c0c0c0 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">Your password: word****</span></span></span></pre>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;">Using the </span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>precision</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> specifier</span></span></span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">$scientific = 1.2e3;<br />
echo sprintf(&#8220;Three decimal places: %.3e&#8221;, $scientific);</span></span></span></p>
<pre style="background: #c0c0c0 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">Three decimal places: 1.200e+3</span></span></span></pre>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">$float = 1.2e3;<br />
echo sprintf(&#8220;Two decimal places: %.2f&#8221;, $float);</span></span></span></p>
<pre style="background: #c0c0c0 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">Two decimal places: 1200.00</span></span></span></pre>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">$string = &#8216;Hello World!&#8217;;<br />
echo sprintf(&#8220;Cut-off after 4 characters: %.4s&#8221;, $string);</span></span></span></p>
<pre style="background: #c0c0c0 none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;"><span style="font-size: medium;">Cut-off after 4 characters: Hell</span></span></span></pre>
<p class="western" style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span style="font-size: medium;"><em>See Also:</em></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;"><span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>printf() –</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> prints a formatted string results rather than simply returning them<br />
</span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>sscanf() –</strong></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><span> Parses a string through a formatted string, reverse of </span></span></span></span></span><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><span lang="en-US"><span style="font-style: normal;"><strong>sprintf()</strong></span></span></span></span></span></p>
<p class="western" style="margin-bottom: 0.07in;" align="center">
<p class="western" style="background: transparent none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<p class="western" style="background: transparent none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<p class="western" style="background: transparent none repeat scroll 0%; margin-bottom: 0.07in; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/samples/sprintf-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Language constructs vs functions in Control Structures</title>
		<link>http://www.phpreferencebook.com/clarifications/language-constructs-vs-functions-in-control-structures/</link>
		<comments>http://www.phpreferencebook.com/clarifications/language-constructs-vs-functions-in-control-structures/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 05:30:56 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Clarifications]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[include]]></category>
		<category><![CDATA[include_once]]></category>
		<category><![CDATA[language construct]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[require]]></category>
		<category><![CDATA[require_once]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=11</guid>
		<description><![CDATA[require(), require_once(), include(), include_once() (pgs. 31-32) Throughout the noted pages and scattered in a few other places, the above four are referred to as functions, when technically they are considered language constructs. While minor, it should still be clarified. All the functionality and examples are accurate, just some terminology clarifications.]]></description>
			<content:encoded><![CDATA[<h3>require(), require_once(), include(), include_once()</h3>
<h4>(pgs. 31-32)</h4>
<p>Throughout the noted pages and scattered in a few other places, the above four are referred to as functions, when technically they are considered language constructs. While minor, it should still be clarified. All the functionality and examples are accurate, just some terminology clarifications.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/clarifications/language-constructs-vs-functions-in-control-structures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
