<?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>Brettic.us &#187; PHP</title>
	<atom:link href="http://brettic.us/category/tech/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://brettic.us</link>
	<description>Web devlopment and life in general</description>
	<lastBuildDate>Fri, 03 Sep 2010 16:06:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Getting the GET back in CodeIgniter!</title>
		<link>http://brettic.us/2010/08/25/getting-the-get-back-into-codeigniter/</link>
		<comments>http://brettic.us/2010/08/25/getting-the-get-back-into-codeigniter/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 06:36:17 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=382</guid>
		<description><![CDATA[Ever wonder why you can't use query strings with CodeIgniter? This post will explain to some degree, but more importantly, show you how to parse query strings and how to get the GET back in CodeIgniter.]]></description>
			<content:encoded><![CDATA[<h4>Ever wonder why you can&#8217;t use query strings with CodeIgniter? This post will explain to some degree, but more importantly, show you how to parse query strings and how to get the GET back in CodeIgniter!</h4>
<p>I&#8217;ve been using CodeIgniter for a couple years now. It&#8217;s small, it&#8217;s simple, it&#8217;s powerful. Admittedly, it&#8217;s not perfect (what framework is?) but it&#8217;s a wonderful way to slap together a website using <a href="http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller">MVC design principles</a> and PHP. I do have one big complaint, however.</p>
<p><span id="more-382"></span></p>
<h3>The Hated $_GET global</h3>
<p>The CodeIgniter developers have some strange obsession with killing all GET data. It appears, from the manual at least, that they <em>strongly</em> discourage you from using query strings. For example, the following manual entry states:</p>
<blockquote><p><strong>By default</strong>, URLs in CodeIgniter are designed to be search-engine and human friendly. <strong>Rather than using the standard &#8220;query string&#8221; approach</strong> to URLs that is synonymous with dynamic systems, <strong>CodeIgniter uses a segment-based approach</strong>:</p></blockquote>
<p>You&#8217;ll notice that I have highlighted the words, &#8220;<strong>By default</strong>,&#8230;&#8221; because you <em>can</em>, in fact, turn on query strings in CodeIgniter, however there is an important trade-off to note (besides saying goodbye to those pretty URL&#8217;s:)</p>
<blockquote><p>Please note: If you are using query strings <strong>you will have to build your own URLs, rather than utilizing the URL helpers</strong> (and other helpers that generate URLs, like some of the form helpers) as these are designed to work with segment based URLs.</p></blockquote>
<p>Okay, admittedly, it&#8217;s not a big deal to build your own URL&#8217;s. I mean we used to do that all the time, right? However, what if you have used the &#8220;default method&#8221; of the segment-based approach and suddenly you need to pass GET values to your CodeIgniter application? Let&#8217;s also say that you&#8217;ve used many of those helpful URL functions, etc. Now, for the cost of enabling query strings, in the manner indicated by the manual, for that one page, you&#8217;ve got plenty of re-factoring to do! Not cool.</p>
<p>I know this sounds like a fantasy scenario, but it&#8217;s real. <em>It happened to me</em>. On two different occasions, working with two different payment gateway API&#8217;s, I suddenly had to provide postback URL&#8217;s. In both instances, the postback URL&#8217;s that were directed to my website, attached query-string variables to the URL in order to provide the relevant data (receipt number, etc.) Obviously, this is something I had no control over because I had to adhere to the API convention and they didn&#8217;t offer alternatives for &#8220;segment-based&#8221; Web frameworks!  I realize that I could have used a standalone script to handle these requests, but that could be potentially confusing for future developers (including myself after a few months) because the standalone script would reside outside of the framework. Also, I have based the entire application on the framework. The framework keeps my code organized in a way. I don&#8217;t want to have to use scripts that do not adhere to my conventions. I want to use the framework for all my code! More importantly, what about my models, libraries, helpers, etc? The framework shouldn&#8217;t force you to make the decision not to use it. It should, at least, provide a way to use a long-establish standard without compromising one of the best benefits of the framework.</p>
<p>I can understand disabling the $_GET super global (and query-string usage) by default when the whole framework is  geared towards segment-based URL&#8217;s. What I can&#8217;t understand is why they  make it so hard to enable query strings for situations when you might  actually need them. There is no simple configuration Boolean variable you can flip on and off!</p>
<p>So, why can&#8217;t we just specify a controller/method URI and tack on a query string? Admittedly, this gets a little complex to depend on with mod_rewrite and other factors outside of the control of the CodeIgniter developers. However, no attempt has been made to allow for the possibility. For an explanation, let&#8217;s go back to the manual:</p>
<blockquote><p>The security filtering function is called automatically when a new controller is invoked. It does the following:</p>
<ul>
<li>Destroys the global GET array. <strong>Since CodeIgniter does not utilize GET strings, there is no reason to allow it.</strong></li>
<li>Destroys all global variables in the event register_globals is turned on.</li>
</ul>
</blockquote>
<p>Okay, <em>clearly</em> there <em>is</em> a reason to allow it! I think we can tally up the real reasons behind destroying query string data to a matter of programmer laziness! I know that sounds harsh, but the manual clearly states (to paraphrase,) &#8220;Let&#8217;s not bother with storing GET data anywhere in the CodeIgniter super object because we don&#8217;t use it anyway. Phew, don&#8217;t have to worry about that anymore!&#8221;</p>
<h3>Let&#8217;s put it back! Methods for enabling $_GET in CodeIgniter</h3>
<p>Luckily, there are a few ways we can use GET variables without having to give up the segment-based approach (and all those nice URL functions!)</p>
<h3>Method 1: Extend The Input Class</h3>
<p>If you want to enable codeigniter to get query string variables globally, you can do it with a few easy steps:</p>
<ol>
<li>Create a MY_Input class in application/libraries</li>
<li>Update your application/config/config.php file: Change $config['uri_protocol']  to  &#8220;PATH_INFO&#8221;</li>
<li>If you are using mod_rewrite to hide index.php, make sure you have a slash in your rule after index.php: RewriteRule ^(.*)$ index.php/$1 [L]</li>
</ol>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">application/libraries/MY_Input.php</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span>
<span class="co1">// this class extension allows for $_GET access</span>
<span class="kw2">class</span> MY_Input <span class="kw2">extends</span> CI_input <span class="br0">&#123;</span>
&nbsp;
    <span class="kw2">function</span> _sanitize_globals<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="co1">// setting allow_get_array to true is</span>
	<span class="co1">// the only real modification</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">allow_get_array</span> <span class="sy0">=</span> <span class="kw4">TRUE</span><span class="sy0">;</span>
&nbsp;
        parent<span class="sy0">::</span>_sanitize_globals<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span>
<span class="coMULTI">/* End of file MY_Input.php */</span>
<span class="coMULTI">/* Location: .application/libraries/MY_Input.php */</span></pre></div></div>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Changes to application/config/config.php</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="coMULTI">/*
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string. The default setting of &quot;AUTO&quot; works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
|
*/</span>
<span class="re0">$config</span><span class="br0">&#91;</span><span class="st_h">'uri_protocol'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st0">&quot;PATH_INFO&quot;</span><span class="sy0">;</span></pre></div></div>
<h3>When using mod_rewrite</h3>
<p>If you&#8217;re hiding index.php with mod_rewrite, make sure you use a slash after index.php. I could never make this work with PATH_INFO configured until I pulled out the question mark (?) in my RewriteRule.</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Slash only. Change ?/  to / if there.</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="apache"><span class="kw1">RewriteRule</span> ^(.*)$ index.php/$<span class="nu0">1</span> [L]</pre></div></div>
<h3>Using the get method of the Input class</h3>
<p>The nice part about this method is that you can refer to get variables using the input class. This has the advantage of easily utilizing the cross-site-scripting tools in the Input class (and keeping with the &#8220;CodeIgniter way&#8221; of doing things.)</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Use the get method of the Input class</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span> <span class="kw1">echo</span> <span class="st_h">'Using Input class with XSS protection: '</span> <span class="sy0">.</span>
	<span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="sy0">-&gt;</span><span class="me1">get</span><span class="br0">&#40;</span><span class="st_h">'foo'</span><span class="sy0">,</span> <span class="kw4">TRUE</span><span class="br0">&#41;</span><span class="sy0">;</span><span class="sy1">?&gt;</span></pre></div></div>
<h3>Conclusion</h3>
<p>I had to do some &#8220;tinkering&#8221; to get this method to work; but in the end, it&#8217;s a fairly simple hack that requires minimal changes.</p>
<p>Our next method requires no configuration changes nor extended libraries whatsoever.</p>
<h3>Method 2: Use parse_str to Grab GET data out of REQUEST_URI</h3>
<p>This method should, perhaps, be subtitled, &#8220;The Quick and Dirty Method&#8221; as it&#8217;s extremely easy to implement: one line of code.</p>
<p>Despite what the manual states, CodeIgniter fails to destroy all global variables. For example, $_SERVER['REQUEST_URI']   shows what was given from the browser as the URL path. (Example: /controller/method?foo=bar.)</p>
<p>Having this data, we can use PHP&#8217;s parse_str function to parse $_SERVER['REQUEST_URI'] and rebuild the $_GET array. We also have to utilize substr to strip away the /controller/method part. Otherwise, we&#8217;ll end up with a $_GET key similar to &#8220;/controller/method?foo&#8221; when we just want our associative array key to be &#8220;foo.&#8221; It&#8217;s important to note that we can still use the fabulous Input class to filter our query-string data (it&#8217;s just not quite as pretty) against cross-site-scripting attacks.</p>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Use parse_str to rebuild $_GET</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span>
<span class="co1">//Remove everything before (and including)</span>
<span class="co1">//the question mark and then parse into $_GET array.</span>
<a href="http://www.php.net/parse_str"><span class="kw3">parse_str</span></a><span class="br0">&#40;</span><a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><a href="http://www.php.net/strrchr"><span class="kw3">strrchr</span></a><span class="br0">&#40;</span><span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st_h">'REQUEST_URI'</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="st0">&quot;?&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="re0">$_GET</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="st_h">' Using rebuilt $_GET array with xss protection: '</span> <span class="sy0">.</span>
	<span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="sy0">-&gt;</span><span class="me1">xss_clean</span><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'foo'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="sy1">?&gt;</span></pre></div></div>
<h3>My preference because it&#8217;s a &#8220;genuine hack&#8221;</h3>
<p>I should note that this is the method I used for my payment gateway postbacks. In my particular situation, this was the best fit because I needed query-string variables for a single URL in the entire site. I really didn&#8217;t want to have to extend CodeIgniter or change my Apache settings for one URL (I&#8217;m fine with segment-based URL&#8217;s as long as they&#8217;re in my control.) I just put this one line of code in my controller method where it was needed.</p>
<p>If you don&#8217;t want the bother of dropping this code into each one of your controller methods, you can easily incorporate this into a helper function and make a simple function call any time you need $_GET. If you want to enable rebuilding $_GET globally, you can also take advantage of setting this code up in a pre_controller hook.</p>
<p>The best part of this method is that it works with CodeIgniter out-of-the-box. No configuration changes or extended libraries should be necessary. Always a &#8220;plus&#8221; in my book.</p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/08/25/getting-the-get-back-into-codeigniter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Analytics Hook for CodeIgniter</title>
		<link>http://brettic.us/2010/08/09/google-analytics-hook-codeigniter/</link>
		<comments>http://brettic.us/2010/08/09/google-analytics-hook-codeigniter/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 22:37:16 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=351</guid>
		<description><![CDATA[So, you finished that CodeIgniter Website last month. It was a tedious project and you&#8217;re glad it&#8217;s over! But wait! Now, your client informs you that you&#8217;re missing all the tracking code! The client also mentions he wants to use the popular, free Analytics tools from Google. This is gonna require inserting some javascript into [...]]]></description>
			<content:encoded><![CDATA[<p>So, you finished that CodeIgniter Website last month. It was a tedious project and you&#8217;re glad it&#8217;s over! But wait! Now, your client informs you that you&#8217;re missing all the tracking code! The client also mentions he wants to use the popular, free Analytics tools from Google. This is gonna require inserting some javascript into all of your Web pages. Not a problem if you used a global footer in your views. A potentially huge pain otherwise. Either way, if you&#8217;re looking for a very simple way to globally implement Analytics code in your application, CodeIgniter hooks can make your life much easier. I&#8217;m going to show you how to take advantage of the &#8216;display_override&#8217; hook point in CodeIgniter to get the job done fast!<span id="more-351"></span></p>
<p>To pull this off, you&#8217;ll need to slightly modify two configuration files (config.php and hooks.php) and then create another with a &#8220;hook&#8221; function (google_analytics.php.)</p>
<p>First, create a file named google_analytics.php and place it inside your application/hooks folder. We&#8217;ll reference this file later when we do our hooks configuration from within application/config/hooks.php.</p>
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">google_analytics.php</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span> <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="sy0">!</span> <a href="http://www.php.net/defined"><span class="kw3">defined</span></a><span class="br0">&#40;</span><span class="st_h">'BASEPATH'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st_h">'No direct script access allowed'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="sy0">!</span><a href="http://www.php.net/function_exists"><span class="kw3">function_exists</span></a><span class="br0">&#40;</span><span class="st_h">'insert_ga_script'</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw2">function</span> insert_ga_script<span class="br0">&#40;</span><span class="re0">$account_id</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="coMULTI">/***NOTE: This hook replaces the _display
        * method in the Output Class. I have
        * not made provisions for caching nor compression!
        */</span>
&nbsp;
        <span class="re0">$CI</span> <span class="sy0">=&amp;</span> get_instance<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="re0">$output</span> <span class="sy0">=</span> <span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">output</span><span class="sy0">-&gt;</span><span class="me1">get_output</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="co1">//keep performance templates in...</span>
        <span class="re0">$elapsed</span> <span class="sy0">=</span> <span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">benchmark</span><span class="sy0">-&gt;</span><span class="me1">elapsed_time</span><span class="br0">&#40;</span><span class="st_h">'total_execution_time_start'</span><span class="sy0">,</span> <span class="st_h">'total_execution_time_end'</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="re0">$output</span> <span class="sy0">=</span> <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st_h">'{elapsed_time}'</span><span class="sy0">,</span> <span class="re0">$elapsed</span><span class="sy0">,</span> <span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$memory</span>	 <span class="sy0">=</span> <span class="br0">&#40;</span> <span class="sy0">!</span> <a href="http://www.php.net/function_exists"><span class="kw3">function_exists</span></a><span class="br0">&#40;</span><span class="st_h">'memory_get_usage'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> ? <span class="st_h">'0'</span> <span class="sy0">:</span> <a href="http://www.php.net/round"><span class="kw3">round</span></a><span class="br0">&#40;</span><a href="http://www.php.net/memory_get_usage"><span class="kw3">memory_get_usage</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">/</span><span class="nu0">1024</span><span class="sy0">/</span><span class="nu0">1024</span><span class="sy0">,</span> <span class="nu0">2</span><span class="br0">&#41;</span><span class="sy0">.</span><span class="st_h">'MB'</span><span class="sy0">;</span>
        <span class="re0">$output</span> <span class="sy0">=</span> <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st_h">'{memory_usage}'</span><span class="sy0">,</span> <span class="re0">$memory</span><span class="sy0">,</span> <span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="co1">// keep profiler working</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">output</span><span class="sy0">-&gt;</span><span class="me1">enable_profiler</span> <span class="sy0">==</span> <span class="kw4">TRUE</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">load</span><span class="sy0">-&gt;</span><span class="me1">library</span><span class="br0">&#40;</span><span class="st_h">'profiler'</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="co1">// If the output data contains closing &lt;/body&gt; and &lt;/html&gt; tags</span>
            <span class="co1">// we will remove them and add them back after we insert the profile data</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st0">&quot;|&lt;/body&gt;.*?&lt;/html&gt;|is&quot;</span><span class="sy0">,</span> <span class="re0">$output</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
                <span class="re0">$output</span>  <span class="sy0">=</span> <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&quot;|&lt;/body&gt;.*?&lt;/html&gt;|is&quot;</span><span class="sy0">,</span> <span class="st_h">''</span><span class="sy0">,</span> <span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="re0">$output</span> <span class="sy0">.=</span> <span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">profiler</span><span class="sy0">-&gt;</span><span class="me1">run</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="re0">$output</span> <span class="sy0">.=</span> <span class="st_h">'&lt;/body&gt;&lt;/html&gt;'</span><span class="sy0">;</span>
            <span class="br0">&#125;</span> <span class="kw1">else</span>  <span class="br0">&#123;</span>
                <span class="re0">$output</span> <span class="sy0">.=</span> <span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">profiler</span><span class="sy0">-&gt;</span><span class="me1">run</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
&nbsp;
        <span class="co1">// if no account information just return the output</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$account_id</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="co1">// keep _output methods working...</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/method_exists"><span class="kw3">method_exists</span></a><span class="br0">&#40;</span><span class="re0">$CI</span><span class="sy0">,</span> <span class="st_h">'_output'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="re0">$CI</span><span class="sy0">-&gt;</span>_output<span class="br0">&#40;</span><span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                <span class="kw1">echo</span> <span class="re0">$output</span><span class="sy0">;</span>  <span class="co1">// Send it to the browser!</span>
            <span class="br0">&#125;</span>
            <span class="kw1">return</span> <span class="sy0">;</span>
        <span class="br0">&#125;</span>
        <a href="http://www.php.net/ob_start"><span class="kw3">ob_start</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="sy1">?&gt;</span>
    &lt;!--Begin Google Analytics Site Code--&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    var gaJsHost = ((&quot;https:&quot; == document.location.protocol) ? &quot;https://ssl.&quot; : &quot;http://www.&quot;);
    document.write(unescape(&quot;%3Cscript src='&quot; + gaJsHost + &quot;google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E&quot;));
    &lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    try {
      var pageTracker = _gat._getTracker(&quot;<span class="sy1">&lt;?=</span><span class="re0">$account_id</span><span class="sy1">?&gt;</span>&quot;);
      pageTracker._initData();
      pageTracker._trackPageview();
    } catch(err) {}
    &lt;/script&gt;
    &lt;!--End Google Analytics Site Code--&gt;
        <span class="kw2">&lt;?php</span>
        <span class="re0">$script</span> <span class="sy0">=</span> <a href="http://www.php.net/ob_get_clean"><span class="kw3">ob_get_clean</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="co1">//insert the script</span>
        <span class="re0">$output</span> <span class="sy0">=</span> <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st_h">'&lt;/html&gt;'</span><span class="sy0">,</span> <span class="re0">$script</span> <span class="sy0">.</span> <span class="st0">&quot;<span class="es1">\n</span>&lt;/html&gt;&quot;</span><span class="sy0">,</span> <span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="co1">// keep _output methods working...</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/method_exists"><span class="kw3">method_exists</span></a><span class="br0">&#40;</span><span class="re0">$CI</span><span class="sy0">,</span> <span class="st_h">'_output'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$CI</span><span class="sy0">-&gt;</span>_output<span class="br0">&#40;</span><span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
            <span class="kw1">echo</span> <span class="re0">$output</span><span class="sy0">;</span>  <span class="co1">// Send it to the browser!</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
<span class="sy1">?&gt;</span></pre></div></div>
<p>The function above slips Analytics javsacript code into the CodeIgniter output buffer. It finds the closing html tag in CodeIgniter&#8217;s output and replaces it with our script. It then puts the closing html tag back into the output string. Simple and effective; because your views generally output html, and this method will only insert Analytics code if a closing html tag is found (your rss feeds, etc. are safe.)</p>
<p>Now, how do we intercept CodeIgniter&#8217;s output before rendering it to the browser? This is where hooks come into play. CodeIgniter hooks provide a way to extend the framework without messing with the CodeIgniter core code (which has many advantages I won&#8217;t go into during this post.) CodeIgniter defines &#8220;hook points&#8221; to allow you to specify where you want to throw your &#8220;monkey wrench.&#8221; Our &#8220;gilded wrench&#8221; is going to be thrown into CodeIgniter&#8217;s display or output proccess.</p>
<p>To demonstrate, let&#8217;s edit our application/config/hooks.php file. We leave &#8216;class&#8217; empty because we only need a function (in case you wondered.) We specify &#8216;googlea.php&#8217; under &#8216;filename.&#8217; We specify our &#8216;function&#8217; (in our case &#8216;insert_track_js.&#8217;)  Finally, we have one parameter to send to our function: the value of the google analytics account number (yours will vary of course.) We hook into the &#8216;display_override&#8217; hook point to override CodeIgniter&#8217;s default output (display) process. When you are done, your application/config/hooks.php should look similar to:</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">hooks.php</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span>  <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="sy0">!</span> <a href="http://www.php.net/defined"><span class="kw3">defined</span></a><span class="br0">&#40;</span><span class="st_h">'BASEPATH'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st_h">'No direct script access allowed'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="coMULTI">/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define &quot;hooks&quot; to extend CI without hacking the core
| files.  Please see the user guide for info:
|
|	http://codeigniter.com/user_guide/general/hooks.html
|
*/</span>
&nbsp;
<span class="co1">// google analytics insertion</span>
<span class="re0">$hook</span><span class="br0">&#91;</span><span class="st_h">'display_override'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span>
    <span class="st_h">'class'</span>    <span class="sy0">=&gt;</span> <span class="st_h">''</span><span class="sy0">,</span>
    <span class="st_h">'function'</span> <span class="sy0">=&gt;</span> <span class="st_h">'insert_ga_script'</span><span class="sy0">,</span>
    <span class="st_h">'filename'</span> <span class="sy0">=&gt;</span> <span class="st_h">'google_analytics.php'</span><span class="sy0">,</span>
    <span class="st_h">'filepath'</span> <span class="sy0">=&gt;</span> <span class="st_h">'hooks'</span><span class="sy0">,</span>
    <span class="st_h">'params'</span>   <span class="sy0">=&gt;</span> <span class="st_h">'UA-*******-**'</span>
<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="coMULTI">/* End of file hooks.php */</span>
<span class="coMULTI">/* Location: ./system/application/config/hooks.php */</span></pre></div></div>
<p>Finally, we just need to enable hooks in application/config/config.php. Look for the &#8216;enable_hooks&#8217; line:</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#config.php"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#config.php"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#config.php" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#config.php" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="re0">$config</span><span class="br0">&#91;</span><span class="st_h">'enable_hooks'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw4">TRUE</span><span class="sy0">;</span></pre></div></div>
<p>Set the value to TRUE as shown above. You&#8217;re done!</p>
<p>For more information on hooks, see the <a href="http://codeigniter.com/user_guide/general/hooks.html">CodeIgniter Manual</a> or <a href="http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-profiling-benchmarking-hooks">this recent tutorial from Net Tuts</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/08/09/google-analytics-hook-codeigniter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to install PHP Zip Extension for MAMP 1.9</title>
		<link>http://brettic.us/2010/07/12/how-to-install-php-zip-extension-in-mamp-1-9/</link>
		<comments>http://brettic.us/2010/07/12/how-to-install-php-zip-extension-in-mamp-1-9/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 05:07:41 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=337</guid>
		<description><![CDATA[Foreword Lately, when attempting to install extensions for MAMP, I&#8217;ve had a difficult time making my extensions work properly because my Darwin ports settings seem to override. Fortunately, I recently found a blog post with instructions that worked. There are a few steps that are different, however, for getting this to work with version 1.9. [...]]]></description>
			<content:encoded><![CDATA[<h2>Foreword</h2>
<p>Lately, when attempting to install extensions for MAMP, I&#8217;ve had a difficult time making my extensions work properly because my <a href="http://serverfault.com/questions/114398/trouble-installing-php-memcache-extension" target="_blank">Darwin ports settings</a> seem to override. Fortunately, I recently found a blog post with instructions that worked. There are a few steps that are different, however, for getting this to work with version 1.9. Therefore, I decided to post the original instructions with those modifications here.<span id="more-337"></span></p>
<h2>Acknowledgments</h2>
<p>The following instructions are modified for MAMP version 1.9 and originally posted by Saverio at <a href="http://developers.enormego.com/view/enabling_php_s_zip_extension_in_mamp" target="_blank">http://developers.enormego.com</a>.</p>
<h2>Instructions</h2>
<ol>
<li><a href="http://sourceforge.net/projects/mamp/files/mamp/1.9/MAMP_components_1.9.dmg/download" target="_blank">Download MAMP&#8217;s &#8220;Source&#8221; package, located here</a>. If the link doesn&#8217;t work, look for and download the MAMP_components_1.9.dmg file located on this page: <a href="http://sourceforge.net/projects/mamp/files/" target="_blank">http://sourceforge.net/projects/mamp/files</a></li>
<li>Mount the DMG image and look for the PHP version that you&#8217;re using (php-5.2.13 or php-5.3.2) and unzip that archive. (Use the Archive Utility. Mine gets extracted to my Downloads folder.)</li>
<li>Open terminal and cd to the PHP directory that you just unzipped (/Users/you/Downloads most likely.)</li>
<li>Run the following command in terminal:<br />
<strong>./configure &#8211;prefix=/tmp/php</strong></li>
<li>Wait for the configure script to finish and then run:<br />
<strong>export PATH=/tmp/php/bin:$PATH</strong></li>
<li>Now, cd to the zip extension directory:<br />
<strong>cd ext/zip</strong></li>
<li>Run:<br />
<strong>phpize</strong></li>
<li>Run:<br />
<strong>./configure</strong></li>
<li>Wait for it to finish and then run:<br />
<strong>make</strong></li>
<li>Wait for it to finish and then run:<br />
<strong>sudo make install</strong> &#8212; (You&#8217;ll need to type your administrative password.)</li>
<li>After it&#8217;s done running, you&#8217;ll see something like this:<br />
Installing shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20060613/</li>
<li>Copy the zip.so extension file from that directory to your PHP installation&#8217;s extension directory:<br />
<strong>cp /usr/lib/php/extensions/no-debug-non-zts-20060613/zip.so /Applications/MAMP/bin/php5.2/lib/php/extensions/no-debug-non-zts-20060613/</strong></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/07/12/how-to-install-php-zip-extension-in-mamp-1-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Form Validation Class using PHP 5.3</title>
		<link>http://brettic.us/2010/06/18/form-validation-class-using-php-5-3/</link>
		<comments>http://brettic.us/2010/06/18/form-validation-class-using-php-5-3/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 06:22:35 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=312</guid>
		<description><![CDATA[Currently, I am working on a minimal MVC framework for a somewhat ambitious project. I chose to host this website using the latest and greatest: PHP-FPM on nginx running PHP 5.3. After realizing that I needed a form validation library, I came up with one that is fairly easy to use and somewhat extensible. This [...]]]></description>
			<content:encoded><![CDATA[<p>Currently, I am working on a minimal MVC framework for a somewhat ambitious project. I chose to host this website  using the latest and greatest: <a href="http://php-fpm.org/">PHP-FPM</a> on <a href="http://nginx.org/en/">nginx</a> running PHP 5.3. After realizing that I needed a form validation library, I came up with one that is fairly easy to use and somewhat extensible. This is the first time I have been able to use Closures in PHP code so maybe I am just a little bit geeky-giddy.</p>
<p><span id="more-312"></span></p>
<h2>The Code</h2>
<p>My only real criteria when I began writing is that I wanted to use method chaining so I could &#8220;stack up&#8221; rules on a given post variable. Here is what I ended up with:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p312code3'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3123"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
</pre></td><td class="code" id="p312code3"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000000; font-weight: bold;">class</span> FormValidator <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$messages</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$rules</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$functions</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$haspostdata</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span>  __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">haspostdata</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'POST'</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">/*** ADD NEW RULE FUNCTIONS BELOW THIS LINE ***/</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * email
     * @param string $message
     * @return FormValidator
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> email<span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'%s is an invalid email address.'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rule</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span> FILTER_VALIDATE_EMAIL<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">FALSE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #009933; font-style: italic;">/**
     * required
     * @param string $message
     * @return FormValidator
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> required<span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'%s is required.'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rule</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">FALSE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * numbersonly
     * @param string $message
     * @return FormValidator
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> numeric<span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'%s must consist of numbers only.'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rule</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[-]?[0-9.]+$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     *
     * @param int $len
     * @param string $message
     * @return FormValidator 
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> minlength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$minlen</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'%s must be at least '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$minlen</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' characters or longer.'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rule</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$minlen</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$minlen</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">FALSE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     *
     * @param int $len
     * @param string $message
     * @return FormValidator
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> maxlength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$maxlen</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'%s must be no longer than '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$maxlen</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' characters.'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rule</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$maxlen</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$maxlen</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">FALSE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     *
     * @param int $len
     * @param string $message
     * @return FormValidator
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> length<span style="color: #009900;">&#40;</span><span style="color: #000088;">$len</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'%s must be exactly '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$len</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' characters in length.'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rule</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$len</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$len</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     *
     * @param string $field
     * @param string $label
     * @param string $message
     * @return FormValidator
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> matches<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$label</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'%s must match '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$label</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$matchvalue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rule</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matchvalue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$matchvalue</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$string</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/*** ADD NEW RULE FUNCTIONS ABOVE THIS LINE ***/</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * callback
     * @param string $name
     * @param mixed $function
     * @param string $message
     * @return FormValidator
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> callback<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/is_callable"><span style="color: #990000;">is_callable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// set rule and function</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rule</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/is_string"><span style="color: #990000;">is_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">FALSE</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// we can parse this as a regexp. set rule function accordingly.</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rule</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>                
                <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// just set a rule function to check equality.</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rule</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$function</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">===</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$function</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * validate
     * @param string $key
     * @param string $label
     * @return bool
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> validate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// do not attempt to validate when no post data is present</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">haspostdata</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// set up field name for error message</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$label</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$label</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">// try each rule function</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rules</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$rule</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$is_true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$is_true</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$function</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">functions</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/is_callable"><span style="color: #990000;">is_callable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #666666; font-style: italic;">// reset rules</span>
                            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rules</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">// reset rules</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rules</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * has_errors
     * @return bool
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> has_errors<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errors</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * set_error_message
     * @param string $rule
     * @param string $message
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> set_error_message<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">messages</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * get_error
     * @param string $field
     * @return string
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errors</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * get_all_errors
     * @return array
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_all_errors<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errors</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * getval
     * @param string $key
     * @return mixed
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getval<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * register_error
     * @param string $rule
     * @param string $key
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> register_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">messages</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'%s has an error.'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$field</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Field with the name of '<span style="color: #006699; font-weight: bold;">$key</span>'&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errors</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * set_rule
     * @param string $rule
     * @param closure $function
     * @param string $message
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> set_rule<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// do not attempt to validate when no post data is present</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">haspostdata</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <a href="http://www.php.net/array_key_exists"><span style="color: #990000;">array_key_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rules</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rules</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <a href="http://www.php.net/array_key_exists"><span style="color: #990000;">array_key_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">functions</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/is_callable"><span style="color: #990000;">is_callable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">functions</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$function</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">messages</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<blockquote><p>Please note: This only works for forms submitted with POST. However, it can be easily modified to work with GET also.</p></blockquote>
<h2>Usage</h2>
<p>Now, let&#8217;s say that I have a form and I want to validate an email field and set up a password. The password must be at least 10 characters and the password confirmation field must match the password field. On top of that, I have a simple captcha where I just ask the user to add two integers that equal 10. With my class, I only need to write the following code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p312code4'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3124"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p312code4"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$formval</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FormValidator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$formval</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">required</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Email Address'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$formval</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">required</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">minlength</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$formval</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">required</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Password Confirmation'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$formval</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">required</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callback</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'captcha'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^\d\s*\+\s*\d$/'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/eval"><span style="color: #990000;">eval</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'%s does not add up to 10!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'captcha'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Captcha'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$formval</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">has_errors</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$formval</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_all_errors</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// submit the form!</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Pretty simple!</p>
<p>I have not started using this class heavily ( I just finished it) so let&#8217;s call it alpha code for now.</p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/06/18/form-validation-class-using-php-5-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging PHP on a Mac with XDebug, Firefox and Netbeans</title>
		<link>http://brettic.us/2009/11/07/developing-php-on-a-mac-with-netbeans/</link>
		<comments>http://brettic.us/2009/11/07/developing-php-on-a-mac-with-netbeans/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 06:07:28 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Netbeans]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=202</guid>
		<description><![CDATA[After nearly two months of using OS X, I think I have finally found my groove when it comes to doing my job. That is, writing code for websites, mainly in PHP. For many years prior to using a Mac for PHP development, I had been using Windows or Linux and Zend Studio 5.x. When [...]]]></description>
			<content:encoded><![CDATA[<p>After nearly two months of using OS X, I think I have finally found my groove when it comes to doing my job.  That is, writing code for websites, mainly in PHP.</p>
<p>For many years prior to using a Mac for PHP development, I had been using Windows or Linux and Zend Studio 5.x. When I was looking several months back for the Mac version download for my Zend Studio license, I was presented, instead, with a free upgrade to version 6. Wow, that was nice Zend! Unfortunately, the version I paid for (the &#8220;perpetual&#8221; license) was no longer there to download.</p>
<blockquote><p>UPDATE: As of my last login to Zend, the 5.5.1 download was available. I installed and everything worked well. Still using Netbeans but appreciated all the same.</p></blockquote>
<p>But hey! I got the brand-new version. That&#8217;s great right? Wrong! Zend decided to drink the Eclipse Kool Aid and completely changed an IDE that I had grown accustomed to (I guess it&#8217;s only fair to point out that Zend Studio 5.x was so old at this point that it was getting increasingly more difficult to install on more modern operating systems and newer java platforms.) In spite of the seemingly &#8220;over-bloat&#8221; of Zend Studio 6.0, I decided to give it a try. I must say that everything just felt wrong. Now I&#8217;m sure there are many PHP developers out there that have been using Eclipse for years and can&#8217;t live without it, but I just couldn&#8217;t seem to make it work my voodoo. I didn&#8217;t have the patience to get my Eclipse PHD, so I went looking elsewhere. That led me to a surprising alternative: <a href="http://www.netbeans.org/features/php/">Netbeans</a>. Although very similar to Eclipse, an IDE with modules/plug-ins for multiple computer language support, Netbeans (entirely free) impressed me right on the spot. I know Zend Studio has all the same features more or less, but the way Netbeans is put together just seemed more natural to me (things I wanted to do actually worked.) Let it suffice to say that I have been using Netbeans since then and I haven&#8217;t looked back!</p>
<p>In this post, we will discover how to go about debugging a specific page in your website through setting up debugging in your MAMP stack, the free Netbeans IDE and your Firefox browser.</p>
<p><span id="more-202"></span></p>
<h2>Setting up Debugging with Xdebug and Firefox</h2>
<p>In order to leverage the new-found versatility or <a href="http://www.mamp.info/en/mamp/index.html">MAMP</a>, I needed a good debugger. I had had a pretty decent debugger with Zend Studio 5.x and that would be an essential requirement for netbeans. One of the cool features of Zend Studio was the integration of the Zend Toolbar with the debugger. Fortunately, this can be done with netbeans also, but you have to get a firefox plugin called <span style="text-decoration: line-through;">Xdebug Helper</span> <a title="easy Xdebug" href="https://addons.mozilla.org/en-US/firefox/addon/58688/" target="_blank">easy Xdebug</a>. When you get it, note that it has one configuration option you need to set: Xdebug.idekey needs to be set to <strong>netbeans-xdebug</strong>.</p>
<p><a href="http://brettic.us/wp-content/uploads/2010/05/Picture-2.png"><img class="size-medium wp-image-298 alignnone" style="margin: 10px; border: 0pt none;" title="easy Xdebug" src="http://brettic.us/wp-content/uploads/2010/05/Picture-2-300x293.png" alt="" width="300" height="293" /></a></p>
<p>Since I was using MAMP, I needed to install the xdebug extension for PHP. I actually downloaded the source and built the extension (used Xcode tools to phpize the source and build it and then just copied it to the appropriate MAMP folder.) I wish I had taken the time to record the steps involved, but instead I&#8217;ll just refer you to <a href="http://debuggable.com/posts/setting-up-xdebug-on-mac-os-x-or-win32-linux:480f4dd6-0240-4a90-8fa1-4e41cbdd56cb">the easy way</a>. Now we just need a little change in our php.ini file and we are good to go:</p>
<blockquote><p>Note: As of MAMP version 1.8.4, there is an xdebug extension that comes with MAMP (just be sure to disable Zend Optimizer since they don&#8217;t seem to play well together.)</p></blockquote>
<p><code>; Xdebug config for Mac OS X and NetBeans IDE<br />
zend_extension=/Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20050922/xdebug.so<br />
xdebug.remote_enable=1<br />
xdebug.remote_handler=dbgp<br />
xdebug.remote_mode=req<br />
xdebug.remote_host=127.0.0.1<br />
xdebug.remote_port=9000</code></p>
<div id="attachment_213" class="wp-caption alignnone" style="width: 310px"><a href="http://brettic.us/wp-content/uploads/2009/11/Picture-3.png"><img class="size-medium wp-image-213" title="Xdebug in php.ini" src="http://brettic.us/wp-content/uploads/2009/11/Picture-3-300x198.png" alt="Xdebug in php.ini" width="300" height="198" /></a><p class="wp-caption-text">Xdebug in php.ini</p></div>
<p>Now, all we have to do in order to debug is evoke the debugger in netbeans. I do this by clicking on the debug project button in the netbeans toolbar:</p>
<div id="attachment_221" class="wp-caption alignnone" style="width: 310px"><a href="http://brettic.us/wp-content/uploads/2009/11/Picture-2.png"><img class="size-medium wp-image-221" title="button" src="http://brettic.us/wp-content/uploads/2009/11/Picture-2-300x92.png" alt="netbeans debug button" width="300" height="92" /></a><p class="wp-caption-text">netbeans debug button</p></div>
<p>Netbeans will open up your development website according to the URL you submitted when creating the project (make sure firefox is your default browser and that you have set the default page in your project.)</p>
<blockquote><p><strong>Tip! </strong>You can also force Netbeans to not open your browser when you debug:</p>
<p style="padding-left: 30px;">In your project properties (right click on the project -&gt;  properties), click on Run Configuration, look for the “Advanced…” button  and set “<strong>Do Not Open Web Browser</strong>“. Click OK.</p>
<p>A big thanks to <a href="http://www.leonardteo.com/2010/08/getting-php-xdebug-codeigniter-and-netbeans-to-all-work-nicely-together/" target="_blank">Leonard Teo</a> for this tip.</p>
</blockquote>
<p>Often, just clicking the button will automatically activate the Xdebug Helper addon for me. But if you want to make sure that netbeans debugs the URL in your browser, be sure the click the X in your firefox footer bar so that it changes color from gray to green.</p>
<div id="attachment_222" class="wp-caption alignnone" style="width: 310px"><a href="http://brettic.us/wp-content/uploads/2009/11/Picture-31.png"><img class="size-medium wp-image-222" title="Xdebug Helper Toogle Icon" src="http://brettic.us/wp-content/uploads/2009/11/Picture-31-300x272.png" alt="Xdebug Helper Toogle Icon" width="300" height="272" /></a><p class="wp-caption-text">Xdebug Helper Toogle Icon</p></div>
<p>Netbeans will open your startup page to something like:<br />
<code><br />
index.php?XDEBUG_SESSION_START=netbeans-xdebug</code></p>
<p>If you are using a PHP framework like <a href="http://codeigniter.com/">Codeigniter</a>, the URL can be a little annoying at first. I have found that I only need to press the continue button to let the start page run it&#8217;s path and then just close that tab. From that point I just leave the debugger running and toggle the Xdebug Helper &#8220;X&#8221; icon when I need to debug any page that I am working on. Couldn&#8217;t be easier!</p>
<h2>Screencast Tutorials</h2>
<p>The following screencasts cover making XDebug and Netbeans work together. It&#8217;s a recap of the text here with some added instruction on setting up an Apache virtual host for development from your computer. Please note, I do not have the ability to be concise when I speak and you may lose patience. My apologies up front. (If you want a condensed version, just watch Part 2 and than skip to Part 6. You might want to watch Part 5 if 6 leaves you scratching your head.) Also, as many people find when they listen to themselves recorded, I find that my voice and accent sounds kind of strange and annoying. That&#8217;s one part me and another part Southern Utah accent. Bear with me if you can. <img src='http://brettic.us/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h3 style="text-align: left;">Part 1: Prerequites for Xdebug and PHP on a Mac</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43318" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43318"></embed></object></p>
<h3>Part 2: Setting up XDebug Helper</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43319" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43319"></embed></object></p>
<h3>Part 3: Setting up an virtualhost apache environment in MAMP for codeigniter</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43320" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43320"></embed></object></p>
<h3>Part 4: Starting to use XDebug in Netbeans</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43322" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43322"></embed></object></p>
<h3>Part 5: Debugging with Netbeans and XDebug</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43323" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43323"></embed></object></p>
<h3>Part 6: Wrapping up by showing a few features of debugging with xdebug in  Netbeans</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43324" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43324"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2009/11/07/developing-php-on-a-mac-with-netbeans/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP: Just for Newbies?</title>
		<link>http://brettic.us/2008/10/03/php-just-for-newbies/</link>
		<comments>http://brettic.us/2008/10/03/php-just-for-newbies/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 23:28:19 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[2000 years ago, it was Christians. When the Christians became the status quo, it was Jews and Muslims. More recently, in America, African Americans were oppressed and reviled. Since reading recent blogs and after hanging out in a few chat rooms with  the "techno-elite", I kinda feel the newest group to be "thrown to the lions" are PHP developers.

]]></description>
			<content:encoded><![CDATA[<h3>Geeks and Gladiators</h3>
<p>Two-thousand years ago, it was conquered soldiers and Christians. Since recently reading some blogs, and after hanging out in a few chat rooms with the &#8220;techno-elite&#8221;, I&#8217;m thinking  the newest group to be &#8220;thrown to the lions&#8221; are PHP developers!</p>
<p>It&#8217;s convincingly apparent if you search the &#8220;blogosphere&#8221; that many &#8220;techies&#8221; think that the phrase <em>PHP programmer</em> is an oxymoron. If you use PHP does that really earn you the despised title of <em>newbie</em>?<br />
<span id="more-24"></span></p>
<p>Why do you ask? Well, just the other day I was hanging out in an obscure chatroom on irc. The channel was the geographically-nearest enthusiast channel for Ubuntu (and I am an Ubuntu enthusiast.) As I was trying to drum up some conversation (mostly because I was bored) I mentioned that I was a PHP developer to stunning silence. Okay?</p>
<p>The next night I log on again, after all, these are supposed to be people that are promoting Ubuntu and usually that encompasses the ability to be friendly and non-condescending. (Please, don&#8217;t get me wrong, I feel that, typically, the Ubuntu community is probably the best community for helping everyone fit in.) So, this time no chat is going on (as usual) and I sit idly by until I see some chatter (pretty standard and I was in a few other channels at the moment also.) One of the users (a very prolific and well-known contributor to Ubuntu in my relative geographic domain) was commenting that he had decided to use a certain application. However, he quipped, &#8220;Too bad it&#8217;s PHP.&#8221; Okay, that got my attention. There&#8217;s a conversation starter. I write, &#8220;Now I see why when I mentioned that I was a PHP developer, the room got really quiet.&#8221; Ha ha. I think I got a LOL or two. More silence. I then express how I&#8217;m always a little surprised that the django and rails crowd seem to feel some contempt for PHP. Longer silence. Finally, the other fellow posts something along the lines that PHP sucks. Mr. &#8220;Ubuntu Loyal&#8221; agrees with a two-worder meant more as a textual &#8220;high five.&#8221; I promptly agree that there is a lot of spaghetti code written by &#8220;newbies&#8221; in PHP, but that it&#8217;s not impossible to write good code using it. To that, I get a very condescending and elitist remark from the other fellow. It was along the lines of, &#8220;If you still use PHP, you are still a newbie.&#8221; What?! Well, that really ticked me off! I have over 9 years of Web development experience.  This means, in my relatively short time in life, I have invested a large portion of my career devoted to writing web software. I consider myself a professional. Who does this elitist punk think he is calling me a &#8220;newbie!?&#8221; when I express my outrage, Ubuntu-man proceeds to tell me, very non-specifically, why PHP sucks. Basically, he tells me nothing new. He later back-pedals somewhat to convey that it&#8217;s the developers preference that really matters. I suppose I&#8217;m still a &#8220;newbie&#8221; by his estimation though. Anyway, I was a bit stunned by this irrational behavior. Was it warranted? Is PHP a truly awful language? Yes, and no.</p>
<h3>PHP Complaints</h3>
<p>PHP was originally envisioned as more of a templating engine. It began its humble roots in this manner just like many other world-changing web technologies. As more time went on, more feature requests were proposed, and more were implemented, until a &#8220;c-like&#8221; (block structured, procedural) language emerged. Now, I will take some liberty to make an assumption or two&#8230;</p>
<p>Because PHP grew as a collection of functions that are useful for Web development, higher-language programming syntax was not incorporated (Okay, assuming done. Moving on&#8230;) One of the chief complaints from the &#8220;python-pretentious&#8221; and the &#8220;ruby-rousers&#8221; is that PHP has no notion of <em>name space</em> (update. PHP 5.3 and 6.0 now have namespace syntax.) Name space is the practice of enveloping related functions and properties under a  common container. The idea is that you can have a save() function in two entirely different name spaces because you can refer to each function with a name-space prefix first. For example, to save a database record, I could call a function named database.save(). That function would not be confused with the whale.save() function by the compiler because &#8220;database&#8221; and &#8220;whale&#8221; are different code containers. PHP haters complain that this forces PHP extension makers to create overly-long names and makes PHP less modular (because every single function is in scope.)</p>
<p>Another complaint is that variables are loosely typed (implicit.) That is to say, if I set a variable to &#8220;hello world&#8221;, PHP automatically assumes a string and converts that variable to a string type. if I set a variable to the numeral 1, PHP assumes I meant an integer&#8230;you get the idea. So what&#8217;s the problem? Well, without going overboard here, let&#8217;s say that my first variable is named $a (PHP variables begin with the dollar sign) and my second  varible is named $b. When you write the expression: $a + $b, because one of the variables is of the string type: $a, PHP converts $a to an integer (0) and the result is just &#8220;1&#8243;. If I change $a to the literal number 2, PHP now makes an integer assumption and adds the two integers to make the result: 3. Critics of PHP complain that implicit typing creates more opportunities for bugs.</p>
<p>I&#8217;m just going to bunch together a few of the most common complaints that remain. PHP has no default caching mechanism. PHP uses anonymous arrays, PHP only has UNIX dates (1970-2038.) There are many other &#8220;band-wagon&#8221; and &#8220;brain-stormed&#8221; reasons. Many of which are deservedly so. Admittedly, It&#8217;s not a perfect language, but does it really hold you back so effectively as to engender contempt?</p>
<p>So yes, PHP has it&#8217;s limitations. Those of us who embrace PHP and use it on a daily basis are aware of PHP&#8217;s short-comings. But, just like any other language with &#8220;snags&#8221; (no programming language is perfect) we have learned to get around them to get the job done. I cannot say at this point that any of the issues complained about has created any significant roadblocks for me. In fact, a significant number of popular free open source web software out there is written in&#8211;you guessed it&#8211;PHP! If it&#8217;s so bad, why do these developers not switch to python or ruby? A few examples are <a href="http://wordpress.org/" target="_blank">drupal</a> and <a href="http://wordpress.org/" target="_blank">wordpress</a> (this blog is powered by <span style="text-decoration: line-through;">drupal</span> wordpress.) So, according to the &#8220;rail-heads&#8221; and &#8220;django-divas&#8221; we are led to believe that PHP programmers, such as the coders for drupal and wordpress are basically ignorant and coding with one brain-lobe tied behind their backs!</p>
<h3>Irrational and unwarranted nonsense!</h3>
<p>It&#8217;s almost to the point of paranoia. It reminds me of the Apple ads trying to convince you that everybody hates Vista. So, don&#8217;t use it or you&#8217;ll be sorry! As much as I would love a mac, I actually have Vista, and I like it! Developers with a few years of good PHP programming behind them actually like it as well!</p>
<p>Again, I promise that clean, easily-refactorable, and intuitive code can be, and is, written in PHP! For example, name space really isn&#8217;t the issue that the rhetoric suggests. I mean, really, how much harder is it to write mysql_connect() as opposed to mysql.connect()? (yes, it&#8217;d just be connect() if name space were declared but still not that big of an issue.) You can emulate name spaces by writing PHP&#8217;s code into your own objects which can be written very clear and concise just like any other high-level language. It all depends on the developer. Certainly not a &#8220;show stopper.&#8221; Furthermore, PHP 5 fixes a lot of the issues that have been criticized in the past. For example, I mentioned implicit types. PHP 5 supports type hinting. The difference is that you can <strong>choose</strong> to use it. If you have something simple you&#8217;re working on, you can <strong>choose</strong> not too. Some people actually consider implicit types as a convenience. I think it&#8217;s very convenient to be able to <strong>choose</strong>. In the several years I have been developing with PHP, rarely has implicit typing been an issue. Maybe the critics are just lazy programmers that need the compiler to babysit them. On that note, I recently read a post from a developer who stated that one of the many reason he hated PHP so bad was the lack of op caching. Even though it&#8217;s not built into the core of PHP, there is a free caching extension called APC. The manual gives you easy steps on how to install it. Again, you have the choice to use it or not. And speaking of extensions, the new PDO database abstraction extension is simply awesome!</p>
<p>I have heard a famous purveyor of a popular web framework rail on PHP citing that &#8220;his way&#8221; is more natural for web development. What he was really saying is that his framework that is built on another language is easier and faster than developing PHP. Unfortunately, he gives the impression that his preferred language is better for Web development than PHP. In reality, he is comparing a framework for rapid web development to a language. Not a fair comparison. While, I agree that the MVC model for web development works well, I think he is being disingenuous in his notion that his framework is the only way to accomplish this &#8220;natural way.&#8221;  For example, there are numerous PHP frameworks that allow you to use the MVC model of development (my favorite is <a href="http://codeigniter.com" target="_blank">codeigniter</a>.)</p>
<h3>No Language is perfect. Even so, PHP is a great choice for beginners and pros alike.</h3>
<p>I do admit that, because PHP doesn&#8217;t enforce a model for development (you can write procedural or object oriented code), programmers with little experience can write pretty awful and almost impossible-to-maintain code. But hey, you can write crappy code in any language!</p>
<p>PHP was designed to be a convenient language to learn and just knock stuff out (email forms, website authentication, etc.) And it performs this amazingly well!  Web applications are more frequently simple than not. I have never felt held back by what I could or could not perform using PHP. I can also note, from a perspective of enterprise, PHP scales up just fine. The large variety of Web applications available that are written in PHP is proof. It all comes down to how smartly they were built. Facebook works just fine folks! Yes, it&#8217;s true, compiled code will always outperform code compiled on-the-fly, but python and ruby aren&#8217;t compiled either (besides, compiled code makes web development a pain in the backside IMHO.)</p>
<p>So yes, PHP may not be a perfect and high-level language. But then again, you don&#8217;t need a nail gun to build a bird house, a hammer works just fine. Don&#8217;t believe the hype! PHP works amazingly well for what it was meant to do. I wear a badge of pride that I call myself a PHP developer, so should you. Stand up for yourself!</p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2008/10/03/php-just-for-newbies/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 4/36 queries in 0.087 seconds using memcached

Served from: brettic.us @ 2010-09-06 06:54:03 -->