<?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; Functions</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>
		<item>
		<title>How to use MySQL REPLACE function</title>
		<link>http://www.php-scripts.com/20070314/101/</link>
		<comments>http://www.php-scripts.com/20070314/101/#comments</comments>
		<pubDate>Wed, 14 Mar 2007 17:02:43 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[How To]]></category>

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

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

		<guid isPermaLink="false">http://www.php-scripts.com/20070314/101/</guid>
		<description><![CDATA[Here&#8217;s a MySQL query fuction that&#8217;s easy to forget about: REPLACE. Let&#8217;s say I have a bunch of records in a field with http://www and want to quickly change all to http://. Here&#8217;s the syntax to update:

UPDATE tdurl_1 SET URL = REPLACE(URL, &#8216;http://www.tdurl.com/&#8217;,'http://tdurl.com/&#8217;);

]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a MySQL query fuction that&#8217;s easy to forget about: REPLACE. Let&#8217;s say I have a bunch of records in a field with http://www and want to quickly change all to http://. Here&#8217;s the syntax to update:</p>
<div class="codesnip-container" >
<div class="codesnip">UPDATE tdurl_1 SET URL = REPLACE(URL, &#8216;http://www.tdurl.com/&#8217;,'http://tdurl.com/&#8217;);</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20070314/101/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Debugging numerical sorting with strings</title>
		<link>http://www.php-scripts.com/20060103/75/</link>
		<comments>http://www.php-scripts.com/20060103/75/#comments</comments>
		<pubDate>Wed, 04 Jan 2006 01:32:41 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[How To]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=75</guid>
		<description><![CDATA[Ahh, good to be back after the holidays. Took a little time off from most the blogs including this one for those who are new readers. 
In the meantime a good question came in on the sorting post back in October from Beregszászi Mihály, who wrote: 

I have got a problem:
rsort($numbers);
Print:
9
8
7
70
6
69
68
67
This is my problem! My [...]]]></description>
			<content:encoded><![CDATA[<p>Ahh, good to be back after the holidays. Took a little time off from most the blogs including this one for those who are new readers. </p>
<p>In the meantime a good question came in on the <a href="http://www.php-scripts.com/20051014/40/">sorting post back in October</a> from Beregszászi Mihály, who wrote: </p>
<blockquote><p>
I have got a problem:<br />
rsort($numbers);</p>
<p>Print:<br />
9<br />
8<br />
7<br />
70<br />
6<br />
69<br />
68<br />
67</p>
<p>This is my problem! My dream:<br />
70<br />
69<br />
68<br />
67<br />
66<br />
65<br />
9<br />
8<br />
7<br />
6
</p></blockquote>
<p>The first thing I did was whip up some example code to test the rsort() and asort() functions. I didn&#8217;t find the situation that he was talking about so I tried to work it backwards and treat the numbers as strings with spaces and possibly returns and other hidden characters in them.</p>
<p>This created the following sort bug that Beregszászi was talking about (and yet he didn&#8217;t offer the code for):</p>
<p>[code=&#8221;html&#8221;]<br />
Reverse sort -> rsort()<br />
9,8 ,70 ,7 ,69 ,68,67,6<br />
[/code]</p>
<p><b>Solution</b><br />
What Beregszászi is looking for is a numerical sort using data that isn&#8217;t in numerical type. To change the type this is called <i>casting</i>. PHP isn&#8217;t as anal as <strike>some</strike> most languages about using the right type but it is critical in <strike>some</strike> most other languages. It&#8217;s best to get in the habit as a programmer as always ensuring the data type matches or you&#8217;ll end up with type mismatch errors (in PHP you&#8217;ll often end up with unexpected results). </p>
<p>First we need to type the data as a number so we can sort it properly. We can do that as follows:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$numbers2</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
<span class="st0">&#8216;9&#8242;</span>,<br />
<span class="st0">&#8216;8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8216;</span>,<br />
<span class="st0">&#8216;7&nbsp; &#8216;</span>,<br />
<span class="st0">&#8216;70<br />
&#8216;</span>,<br />
<span class="st0">&#8216;6&#8242;</span>,<br />
<span class="st0">&#8216;69</p>
<p>&#8216;</span>,<br />
<span class="st0">&#8216;68&#8242;</span>,<br />
<span class="st0">&#8216;67&#8242;</span><span class="br0">&#41;</span>;</p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;hr&gt;Jumbled order (STRING TEST)&lt;br /&gt;&#8221;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="br0">&#40;</span><a href="http://www.php.net/join"><span class="kw3">join</span></a><span class="br0">&#40;</span><span class="st0">&#8220;,&#8221;</span>,<span class="re0">$numbers2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">// begin code to cast array contents from string to numeric</span><br />
<span class="re0">$sizeof</span> = <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$numbers2</span><span class="br0">&#41;</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$i</span>=<span class="nu0">0</span>;<span class="re0">$i</span>&lt; <span class="re0">$sizeof</span>;<span class="re0">$i</span>++<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$numbers2</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span> = <span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="re0">$numbers2</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span>;&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span><br />
<span class="co1">// end code to cast array contents from string to numeric</span></p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;br /&gt;Jumbled order (STRING TEST after casting)&lt;br /&gt;&#8221;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="br0">&#40;</span><a href="http://www.php.net/join"><span class="kw3">join</span></a><span class="br0">&#40;</span><span class="st0">&#8220;,&#8221;</span>,<span class="re0">$numbers2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p><a href="http://www.php.net/rsort"><span class="kw3">rsort</span></a><span class="br0">&#40;</span><span class="re0">$numbers2</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;br /&gt;Reverse sort -&gt; rsort()&lt;br /&gt; &#8220;</span> . <span class="br0">&#40;</span><a href="http://www.php.net/join"><span class="kw3">join</span></a><span class="br0">&#40;</span><span class="st0">&#8220;,&#8221;</span>,<span class="re0">$numbers2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;br /&gt;Regular order -&gt; asort()&lt;br /&gt;&#8221;</span>;<br />
<a href="http://www.php.net/asort"><span class="kw3">asort</span></a><span class="br0">&#40;</span><span class="re0">$numbers2</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="br0">&#40;</span><a href="http://www.php.net/join"><span class="kw3">join</span></a><span class="br0">&#40;</span><span class="st0">&#8220;,&#8221;</span>,<span class="re0">$numbers2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Now it will sort as the &#8220;dream&#8221; option he requested.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20060103/75/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to pluralize using PHP</title>
		<link>http://www.php-scripts.com/20051210/71/</link>
		<comments>http://www.php-scripts.com/20051210/71/#comments</comments>
		<pubDate>Sat, 10 Dec 2005 17:31:32 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[How To]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=71</guid>
		<description><![CDATA[This is one of those technical nitpick things. Check out the screenshot below of the new Yahoo Answers and see if you can spot the error. 

The title of this thread kind of gives it away. Note the &#8220;1 Answers&#8221; should actually read &#8220;1 answer&#8221; and then when there ar 2 or more it should [...]]]></description>
			<content:encoded><![CDATA[<p>This is one of those technical nitpick things. Check out the screenshot below of the new <a href="http://answers.yahoo.com/">Yahoo Answers</a> and see if you can spot the error. </p>
<p><img src="http://www.php-scripts.com/images/2005/yahooanswers-pluralizing.jpg" border="0" ALT="What is the error in this screenshot from Yahoo Answers?"/></p>
<p>The title of this thread kind of gives it away. Note the &#8220;1 Answers&#8221; should actually read &#8220;1 answer&#8221; and then when there ar 2 or more it should be pluralized. Let&#8217;s build some quick and dirty pluralizing code using PHP.</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw2">function</span> pluralize<span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$string</span>,<span class="nu0">0</span>,<span class="nu0">1</span><span class="br0">&#41;</span> &gt; <span class="nu0">1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">return</span> <span class="re0">$string</span> . <span class="st0">&#8217;s&#8217;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="kw1">return</span> <span class="re0">$string</span>;<br />
<span class="br0">&#125;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span>pluralize<span class="br0">&#40;</span><span class="st0">&#8216;1 Answer&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;br /&gt;&#8217;</span>;<br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span>pluralize<span class="br0">&#40;</span><span class="st0">&#8216;2 Answer&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>The example above seems to work great, but there is a bug in it. What if you pass this string:</p>
<div class="codesnip-container" >
<div class="codesnip"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span>pluralize<span class="br0">&#40;</span><span class="st0">&#8216;14 Answer&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</div>
<p>The answer is it will return the wrong answer (14 answer, not plural) because only the first character is being evaluated. We need to strip out the number in the beginning first instead of using substr. Here&#8217;s the corrected code.</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw2">function</span> pluralize<span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st0">&#8220;|^([0-9]+)|&#8221;</span>,<span class="re0">$string</span>,<span class="re0">$matched</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$matched</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &gt; <span class="nu0">1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">return</span> <span class="re0">$string</span> . <span class="st0">&#8217;s&#8217;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="kw1">return</span> <span class="re0">$string</span>;<br />
<span class="br0">&#125;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span>pluralize<span class="br0">&#40;</span><span class="st0">&#8216;1 Answer&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;br /&gt;&#8217;</span>;<br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span>pluralize<span class="br0">&#40;</span><span class="st0">&#8216;14 Answer&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;br /&gt;&#8217;</span>;<br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span>pluralize<span class="br0">&#40;</span><span class="st0">&#8216;144 Answer&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>This will return the following:</p>
<div class="codesnip-container" >
<div class="codesnip">1 Answer<br />
14 Answers<br />
144 Answers</div>
</div>
<p>If you would like to add commas just use the built-in number_format() function like this:</p>
<div class="codesnip-container" >
<div class="codesnip"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span><a href="http://www.php.net/number_format"><span class="kw3">number_format</span></a><span class="br0">&#40;</span>pluralize<span class="br0">&#40;</span><span class="st0">&#8216;1456 Answer&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="co1">// returns 1,456 </span><br />
&nbsp;</div>
</div>
<p>Oops, that dropped the answer, didn&#8217;t it? So we need to include in the pluralize() function like this:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw2">function</span> pluralize<span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st0">&#8220;|^([0-9]+)(.*)|&#8221;</span>,<span class="re0">$string</span>,<span class="re0">$matched</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$matched</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &gt; <span class="nu0">1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">return</span> <a href="http://www.php.net/number_format"><span class="kw3">number_format</span></a><span class="br0">&#40;</span><span class="re0">$matched</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span> . <span class="re0">$matched</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> . <span class="st0">&#8217;s&#8217;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="kw1">return</span> <span class="re0">$string</span>;<br />
<span class="br0">&#125;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span>pluralize<span class="br0">&#40;</span><span class="st0">&#8216;1456 Answer&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Now we&#8217;ll get 1,456 Answers pluralized.  </p>
<p><b>Other solutions</b><br />
Here&#8217;s <a href="http://boulter.com/blog/2005/04/22/5-ways-to-pluralize/">another way to pluralize</a> that requires passing a number and arguments in various languages.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051210/71/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Converting domains to cell phone numbers</title>
		<link>http://www.php-scripts.com/20051205/70/</link>
		<comments>http://www.php-scripts.com/20051205/70/#comments</comments>
		<pubDate>Mon, 05 Dec 2005 19:42:17 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[How To]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=70</guid>
		<description><![CDATA[Awhile back I created a basic webmaster utility script called Easy Cell Code Generator which helped find easy to type cell phone numbers, but what if you had a domain and wanted to convert it out the other way? I decided to whip up some quick code to do just that:
Short domain:
mughd.com 
Typed into cell [...]]]></description>
			<content:encoded><![CDATA[<p>Awhile back I created a basic webmaster utility script called <a href="http://www.tdscripts.com/webmaster_utilities/easy_cell.php">Easy Cell Code Generator</a> which helped find easy to type cell phone numbers, but what if you had a domain and wanted to convert it out the other way? I decided to whip up some quick code to do just that:</p>
<p><b>Short domain</b>:<br />
mughd.com </p>
<p><b>Typed into cell phone / portable device as:</b><br />
6-88-4-44-3 [char] 222-666-6</p>
<p>I used [char] because how to type in a period can vary depending on the device. This counts as only one character. That&#8217;s a total of 15 characters to type in a five character domain.</p>
<p>Let&#8217;s create a script to automatically perform the same checks. We&#8217;ll start with a 26 character array to define the code for each character.</p>
<div class="codesnip-container" >< ?php<br />
$test_in = 'mughd.com'; // change to domain you want to use<br />
$portable_matrix = array('a'=>2,&#8217;b'=>22,&#8217;c'=>222,&#8217;d'=>3,&#8217;e'=>33,&#8217;f'=>333,<br />
   &#8216;g&#8217;=>4, &#8216;h&#8217;=>44, &#8216;i&#8217;=>444, &#8216;j&#8217;=>5, &#8216;k&#8217;=>55, &#8216;l&#8217;=>555, &#8216;m&#8217;=>6, &#8216;n&#8217;=>66, &#8216;o&#8217;=>666, &#8216;p&#8217;=>7, &#8216;q&#8217;=>77, &#8216;r&#8217;=>777, &#8217;s&#8217;=>7777, &#8216;t&#8217;=>8, &#8216;u&#8217;=>88, &#8216;v&#8217;=>888, &#8216;w&#8217;=>9, &#8216;x&#8217;=>99, &#8216;y&#8217;=>999, &#8216;z&#8217;=>9999);<br />
$length = strlen($test_in);<br />
$char_code = &#8221;;<br />
if($length>1) {<br />
  for($i=0;$i< $length;$i++) {<br />
       // get character<br />
      $character = strtolower(substr($test_in,$i,1));<br />
	if(preg_match("|[a-z]|",$character)) {<br />
		$char_code .= $portable_matrix[$character];<br />
		$char_no_space .= $portable_matrix[$character] . '-';<br />
		$new_len = $new_len + strlen($portable_matrix[$character]);</p>
<p>	} else {<br />
		$char_code .= '[code]';<br />
		$char_no_space .= '[code]-';<br />
		$new_len = $new_len + 1;<br />
	}<br />
	print "$character -> $char_no_space<br />&#8220;;<br />
  }<br />
$char_no_space = substr($char_no_space,0,-1);<br />
print &#8220;&lt;hr /&gt;&lt;font color=&#8217;green&#8217;&gt;$test_in&lt;/font&gt; [phone characters: $new_len -> domain characters: $length]<br />CHAR CODE (without spaces): <b>$char_code</b><br />CHAR CODE: <b>$char_no_space</b>&#8220;;<br />
}<br />
?></div>
<p>This will produce output that looks like this:</p>
<div class="codesnip-container" >m -> 6-<br />
u -> 6-88-<br />
g -> 6-88-4-<br />
h -> 6-88-4-44-<br />
d -> 6-88-4-44-3-<br />
. -> 6-88-4-44-3-[code]-<br />
c -> 6-88-4-44-3-[code]-222-<br />
o -> 6-88-4-44-3-[code]-222-666-<br />
m -> 6-88-4-44-3-[code]-222-666-6-</p>
<p>mughd.com [phone characters: 15 -> domain characters: 9]<br />
CHAR CODE (without spaces): 6884443[code]2226666<br />
CHAR CODE: 6-88-4-44-3-[code]-222-666-6</p></div>
<p>By changing the $test_in to a different domain you can test different codes. I created a form version where you can just enter in the url at TD Scripts in the Webmaster Utilities area here: http://www.tdscripts.com/webmaster_utilities/easy_cell2.php</p>
<p>Argh, it seems like the code snippet plugin is eating some of the code again. For the HTML portion view the source code of this page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051205/70/feed/</wfw:commentRss>
		</item>
		<item>
		<title>pathinfo</title>
		<link>http://www.php-scripts.com/20051122/59/</link>
		<comments>http://www.php-scripts.com/20051122/59/#comments</comments>
		<pubDate>Wed, 23 Nov 2005 01:36:03 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Functions]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=59</guid>
		<description><![CDATA[Let&#8217;s say you have a long URL or file and want to access different parts. PHP has a built-in function called pathinfo() that may be of assistance. Let&#8217;s look at a couple examples.
File path
$file_path = &#8216;/usr/web/tdavid/php-scripts.com/main/index.html&#8217;;
URL
$url_path = &#8216;http://www.php-scripts.com/main/index.html&#8217;;
Now let&#8217;s dump this into a script and see the output:

&#60;?php
//File path
$file_path = pathinfo&#40;&#8216;/usr/web/tdavid/php-scripts.com/main/index.html&#8217;&#41;;
//URL
$url_path = pathinfo&#40;&#8216;http://www.php-scripts.com/main/index.html&#8217;&#41;;
print &#8216;dirname: &#8216; [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you have a long URL or file and want to access different parts. PHP has a built-in function called pathinfo() that may be of assistance. Let&#8217;s look at a couple examples.</p>
<p>File path<br />
$file_path = &#8216;/usr/web/tdavid/php-scripts.com/main/index.html&#8217;;</p>
<p>URL<br />
$url_path = &#8216;http://www.php-scripts.com/main/index.html&#8217;;</p>
<p>Now let&#8217;s dump this into a script and see the output:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="co1">//File path</span><br />
<span class="re0">$file_path</span> = <a href="http://www.php.net/pathinfo"><span class="kw3">pathinfo</span></a><span class="br0">&#40;</span><span class="st0">&#8216;/usr/web/tdavid/php-scripts.com/main/index.html&#8217;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//URL</span><br />
<span class="re0">$url_path</span> = <a href="http://www.php.net/pathinfo"><span class="kw3">pathinfo</span></a><span class="br0">&#40;</span><span class="st0">&#8216;http://www.php-scripts.com/main/index.html&#8217;</span><span class="br0">&#41;</span>;</p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;dirname: &#8216;</span> . <span class="re0">$file_path</span><span class="br0">&#91;</span><span class="st0">&#8216;dirname&#8217;</span><span class="br0">&#93;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;br /&gt;basename: &#8216;</span> . <span class="re0">$file_path</span><span class="br0">&#91;</span><span class="st0">&#8216;basename&#8217;</span><span class="br0">&#93;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;br /&gt;extension: &#8216;</span> . <span class="re0">$file_path</span><span class="br0">&#91;</span><span class="st0">&#8216;extension&#8217;</span><span class="br0">&#93;</span>;</p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;hr /&gt;&#8217;</span>;</p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;dirname: &#8216;</span> . <span class="re0">$url_path</span><span class="br0">&#91;</span><span class="st0">&#8216;dirname&#8217;</span><span class="br0">&#93;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;br /&gt;basename: &#8216;</span> . <span class="re0">$url_path</span><span class="br0">&#91;</span><span class="st0">&#8216;basename&#8217;</span><span class="br0">&#93;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;br /&gt;extension: &#8216;</span> . <span class="re0">$url_path</span><span class="br0">&#91;</span><span class="st0">&#8216;extension&#8217;</span><span class="br0">&#93;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>This will produce output that looks like the following:</p>
<div class="codesnip-container" >
<div class="codesnip">dirname: /usr/web/tdavid/php-scripts.com/main<br />
basename: index.html<br />
extension: html<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
dirname: http://www.php-scripts.com/main<br />
basename: index.html<br />
extension: html</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051122/59/feed/</wfw:commentRss>
		</item>
		<item>
		<title>ASCII character codes</title>
		<link>http://www.php-scripts.com/20051110/61/</link>
		<comments>http://www.php-scripts.com/20051110/61/#comments</comments>
		<pubDate>Thu, 10 Nov 2005 21:46:30 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[How To]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=61</guid>
		<description><![CDATA[If you need to print out special characters in PHP you will probably want to get familiar with the built-in chr() function. Here&#8217;s some code that will demonstrate the various ASCII character codes available and how to output them to the screen:

&#60;?php
print &#8216;Copyright (169): &#8216; . chr&#40;169&#41;;
print &#8216;&#60;br /&#62;Registered Trademark (174): &#8216; . chr&#40;174&#41;;
print &#8216;&#60;br [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to print out special characters in PHP you will probably want to get familiar with the built-in chr() function. Here&#8217;s some code that will demonstrate the various ASCII character codes available and how to output them to the screen:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;Copyright (169): &#8216;</span> . <a href="http://www.php.net/chr"><span class="kw3">chr</span></a><span class="br0">&#40;</span><span class="nu0">169</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;br /&gt;Registered Trademark (174): &#8216;</span> . <a href="http://www.php.net/chr"><span class="kw3">chr</span></a><span class="br0">&#40;</span><span class="nu0">174</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;br /&gt;Trademark (153): &#8216;</span> . <a href="http://www.php.net/chr"><span class="kw3">chr</span></a><span class="br0">&#40;</span><span class="nu0">153</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;br /&gt;&#8217;</span>;</p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$i</span>=<span class="nu0">1</span>; <span class="re0">$i</span>&amp;lt;<span class="nu0">256</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;$i &lt;b&gt;&#8221;</span> . <a href="http://www.php.net/chr"><span class="kw3">chr</span></a><span class="br0">&#40;</span><span class="re0">$i</span><span class="br0">&#41;</span> . <span class="st0">&#8216;&lt;/b&gt; &#8216;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p><a href="http://www.php-scripts.com/examples/chr.php">Working example here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051110/61/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using file_get_contents before v4.3.0</title>
		<link>http://www.php-scripts.com/20051107/58/</link>
		<comments>http://www.php-scripts.com/20051107/58/#comments</comments>
		<pubDate>Mon, 07 Nov 2005 20:09:44 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Backwards compatibility]]></category>

		<category><![CDATA[How To]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=58</guid>
		<description><![CDATA[One of the dangers of using newer PHP functions is that sometimes you&#8217;ll end up with people who are stuck on a shared host using an older version of PHP. In this case it&#8217;s helpful to have some backwards compatible functions. The category backwards compatability will deal with these situations.
file_get_contents has only been available in [...]]]></description>
			<content:encoded><![CDATA[<p>One of the dangers of using newer PHP functions is that sometimes you&#8217;ll end up with people who are stuck on a shared host using an older version of PHP. In this case it&#8217;s helpful to have some backwards compatible functions. The category backwards compatability will deal with these situations.</p>
<p>file_get_contents has only been available in PHP since 4.3.0. Here is how to make it backwards compatible to version 3.xx of PHP by using file and implode:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="co1">// PHP 4.3.0 or later </span><br />
<span class="re0">$page_source</span> = <a href="http://www.php.net/file_get_contents"><span class="kw3">file_get_contents</span></a><span class="br0">&#40;</span><span class="st0">&#8220;http://www.yourdomain.com/yourpage.html&#8221;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">// pre 4.3.0</span><br />
<span class="re0">$page_source</span> = <a href="http://www.php.net/implode"><span class="kw3">implode</span></a><span class="br0">&#40;</span><span class="st0">&#8221;</span>, <a href="http://www.php.net/file"><span class="kw3">file</span></a><span class="br0">&#40;</span><span class="st0">&#8220;http://www.yourdomain.com/yourpage.html&#8221;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Now let&#8217;s say you wanted to make your own file_get_contents function for pre 4.3.0? Here&#8217;s what you would do:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="co1">// only use this in pre 4.3.0 PHP where file_get_contents is not available</span><br />
<span class="kw2">function</span> <a href="http://www.php.net/file_get_contents"><span class="kw3">file_get_contents</span></a><span class="br0">&#40;</span><span class="re0">$in</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">return</span>&nbsp; <a href="http://www.php.net/implode"><span class="kw3">implode</span></a><span class="br0">&#40;</span><span class="st0">&#8221;</span>, <a href="http://www.php.net/file"><span class="kw3">file</span></a><span class="br0">&#40;</span><span class="re0">$in</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="re0">$page_source</span> = <a href="http://www.php.net/file_get_contents"><span class="kw3">file_get_contents</span></a><span class="br0">&#40;</span><span class="st0">&#8216;http://www.yourdomain.com/yourpage.html&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051107/58/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Is number odd or even?</title>
		<link>http://www.php-scripts.com/20051104/57/</link>
		<comments>http://www.php-scripts.com/20051104/57/#comments</comments>
		<pubDate>Fri, 04 Nov 2005 18:13:20 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[one liners]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=57</guid>
		<description><![CDATA[Testing whether a number is odd or even in PHP. The custom function is_odd() below is inspired by an algorithms thread at Devshed. The function isn&#8217;t even necessary and could be expressed as a one-liner like this:

echo&#40;$number &#38; 1&#41;; // $number = any integer, 0 = even, 1 = odd 
&#160;

Here&#8217;s the code to check [...]]]></description>
			<content:encoded><![CDATA[<p>Testing whether a number is odd or even in PHP. The custom function is_odd() below is inspired by an <a href="http://forums.devshed.com/archive/t-29843/Quick-algorithm-to-determine-oddeven-numbers*/">algorithms thread at Devshed</a>. The function isn&#8217;t even necessary and could be expressed as a one-liner like this:</p>
<div class="codesnip-container" >
<div class="codesnip"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span><span class="re0">$number</span> &amp; <span class="nu0">1</span><span class="br0">&#41;</span>; <span class="co1">// $number = any integer, 0 = even, 1 = odd </span><br />
&nbsp;</div>
</div>
<p>Here&#8217;s the code to check the hours in the day starting with 0 (midnight) and ending at midnight (23):</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">function</span> is_odd<span class="br0">&#40;</span><span class="re0">$number</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">return</span> <span class="re0">$number</span> &amp; <span class="nu0">1</span>; <span class="co1">// 0 = even, 1 = odd</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">foreach</span><span class="br0">&#40;</span><a href="http://www.php.net/range"><span class="kw3">range</span></a><span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">23</span><span class="br0">&#41;</span> <span class="kw1">as</span> <span class="re0">$number</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;$number &#8220;</span> . is_odd<span class="br0">&#40;</span><span class="re0">$number</span><span class="br0">&#41;</span> . <span class="st0">&#8216;&lt;br /&gt;&#8217;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Notes: The range function is handy for prefilling with a string or array with numbers. Now what if you want to show alternating messages, one on the even hours and one on odd hours? The following code will do just that if you change the print statement to be whatever you want to alternate:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw2">function</span> is_odd<span class="br0">&#40;</span><span class="re0">$number</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">return</span> <span class="re0">$number</span> &amp; <span class="nu0">1</span>; <span class="co1">// 0 = even, 1 = odd</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">if</span><span class="br0">&#40;</span>is_odd<span class="br0">&#40;</span><a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;H&#8221;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;Since it&#8217;s an odd hour, I&#8217;ve got an odd message for you: read and be happy&#8221;</span>;<br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;It&#8217;s an even hour, Steven.&#8221;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p><b>Update 10/9/08 7:16am PST</b>: Thanks to several commenters below who pointed out that there was a missing &#8216;)&#8217; &#8212; it has been added now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051104/57/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Brief explanation of how to use arguments and globals inside functions</title>
		<link>http://www.php-scripts.com/20051017/42/</link>
		<comments>http://www.php-scripts.com/20051017/42/#comments</comments>
		<pubDate>Mon, 17 Oct 2005 19:21:41 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Functions]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=42</guid>
		<description><![CDATA[The concept of globalization, functions and arguments can be confusing to those who are new to programming. It&#8217;s not too complicated if you look at a very basic example. The following function isn&#8217;t very practical, but it works.

&#60;?php
function addThis&#40;$a,$b&#41; &#123;
return $a + $b;
&#125;
$sum_of_these = addThis&#40;8,3&#41;; // assigns the value of 11 to the variable $sum_of_these
?&#62;

In [...]]]></description>
			<content:encoded><![CDATA[<p>The concept of globalization, functions and arguments can be confusing to those who are new to programming. It&#8217;s not too complicated if you look at a very basic example. The following function isn&#8217;t very practical, but it works.</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw2">function</span> addThis<span class="br0">&#40;</span><span class="re0">$a</span>,<span class="re0">$b</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="kw1">return</span> <span class="re0">$a</span> + <span class="re0">$b</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="re0">$sum_of_these</span> = addThis<span class="br0">&#40;</span><span class="nu0">8</span>,<span class="nu0">3</span><span class="br0">&#41;</span>; <span class="co1">// assigns the value of 11 to the variable $sum_of_these</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>In the above example we are passing by argument the numbers 8 and 3 to the function called addThis and that function is returning the value of 11 (which is 8 + 3) and then assigns the value of 11 to $sum_of_these variable. This would be the same as writing the following without using any function:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$sum_of_these</span> = <span class="nu0">8</span> + <span class="nu0">3</span>; <span class="co1">// or $sum_of_these = 11; </span><br />
&nbsp;</div>
</div>
<p>Now, let&#8217;s say we want to add in a third variable, called $c to the equation. How can we do this in the function addThis without passing it as an argument? We do this by making the $c variable globally available inside the function addThis like this:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw2">function</span> addThis<span class="br0">&#40;</span><span class="re0">$a</span>,<span class="re0">$b</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<a href="http://www.php.net/global"><span class="kw3">global</span></a> <span class="re0">$c</span>; <span class="co1">// separate multiple globals by a comma like this: global $c, $d, $e;</span><br />
<span class="kw1">return</span> <span class="re0">$a</span> + <span class="re0">$b</span> + <span class="re0">$c</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="re0">$c</span> = <span class="nu0">4</span>; <span class="co1">// first must assign some value to $c</span><br />
<span class="re0">$sum_of_these</span> = addThis<span class="br0">&#40;</span><span class="nu0">8</span>,<span class="nu0">3</span><span class="br0">&#41;</span>; <span class="co1">// assigns the value of 15 to the variable $sum_of_these</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Now let&#8217;s say we don&#8217;t want to use any arguments at all, let&#8217;s globalize $a, $b and $c. This code would look as follows:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw2">function</span> addThis<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<a href="http://www.php.net/global"><span class="kw3">global</span></a> <span class="re0">$a</span>,<span class="re0">$b</span>,<span class="re0">$c</span>; <span class="co1">// separate multiple globals by a comma and end with a semi-colon</span><br />
<span class="kw1">return</span> <span class="re0">$a</span> + <span class="re0">$b</span> + <span class="re0">$c</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="re0">$a</span> = <span class="nu0">8</span>;<br />
<span class="re0">$b</span> = <span class="nu0">3</span>;<br />
<span class="re0">$c</span> = <span class="nu0">4</span>;<br />
<span class="re0">$sum_of_these</span> = addThis<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// assigns the value of 15 to the variable $sum_of_these</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Still the use of a function above doesn&#8217;t make practical sense, but it shows a very rudimentary example of how to use functions, arguments and globals. Now let&#8217;s make a better example of a simple calculator:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw2">function</span> simpleCalc<span class="br0">&#40;</span><span class="re0">$operator</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<a href="http://www.php.net/global"><span class="kw3">global</span></a> <span class="re0">$a</span>,<span class="re0">$b</span>,<span class="re0">$c</span>; <span class="co1">// separate multiple globals by a comma like this: global $c, $d, $e;</span><br />
<span class="kw1">switch</span><span class="br0">&#40;</span><span class="re0">$operator</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">case</span> <span class="st0">&#8216;-&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">return</span> <span class="re0">$a</span> - <span class="re0">$b</span> - <span class="re0">$c</span>;<br />
&nbsp; &nbsp;<span class="kw1">break</span>;<br />
&nbsp; &nbsp;<span class="kw1">case</span> <span class="st0">&#8216;*&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$a</span> * <span class="re0">$b</span> * c;<br />
&nbsp; &nbsp;<span class="kw1">break</span>;<br />
&nbsp; &nbsp;<span class="kw1">case</span> <span class="st0">&#8216;/&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$a</span> / <span class="re0">$b</span> / <span class="re0">$c</span>;<br />
&nbsp; &nbsp;<span class="kw1">break</span>;<br />
&nbsp; &nbsp;<span class="kw2">default</span>:<br />
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$a</span> + <span class="re0">$b</span> + <span class="re0">$c</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="re0">$a</span> = <span class="nu0">8</span>;<br />
<span class="re0">$b</span> = <span class="nu0">3</span>;<br />
<span class="re0">$c</span> = <span class="nu0">4</span>;<br />
<span class="re0">$result</span> = simpleCalc<span class="br0">&#40;</span><span class="st0">&#8216;+&#8217;</span><span class="br0">&#41;</span>; <span class="co1">// assigns the value of 15 to the variable $sum_of_these</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>The above example allows us the flexibility of sending along any three numbers and either add, subtract, multiply or divide.  I used a switch statement to illustrate how to check for addition (+), subtraction (-), multiplication (*) or division (/). Readers will note that the default switch is addition, so there is no need for a case block for that.</p>
<p>So if we wanted to multiply the three variables we&#8217;d use the following code:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$result</span> = simpleCalc<span class="br0">&#40;</span><span class="st0">&#8216;*&#8217;</span><span class="br0">&#41;</span>; <span class="co1">// $result = 8 x 3 x 4 = 96 </span><br />
&nbsp;</div>
</div>
<p>Subtraction, just pass along the &#8216;-&#8217; like this:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$result</span> = simpleCalc<span class="br0">&#40;</span><span class="st0">&#8216;-&#8217;</span><span class="br0">&#41;</span>; <span class="co1">// $result = 8 - 3 - 4 = 1 </span><br />
&nbsp;</div>
</div>
<p>This is a real crude and impractical calculator because a user could enter in a combination of more than three numbers and even multiple operators, but it clearly demonstrates how to pass and return arguments in functions, how to globalize variables and how to perform different operations based on conditions. At the most basic level that is all a calculator does: it takes input and processes.</p>
<p>I&#8217;ll leave this type of calculator for a more advanced tutorial (a Part 2, perhaps) or maybe readers would like to build their own? Basically to accomplish a full calculator, the programmer would need to dump all numbers and operators into an array and work through that in the function to return the value. This might be kind of fun for a quick and basic JavaScript app. Don&#8217;t even need server side PHP code for this, it could be done entirely in JavaScript in a step-by-step process because a calculator is somethng most everybody understands.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051017/42/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
