<?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>bradley proctor</title>
	<atom:link href="http://www.bradleyproctor.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bradleyproctor.com</link>
	<description>Thoughts and writings</description>
	<lastBuildDate>Thu, 15 Mar 2012 16:26:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Apache, PHP, Windows, and php_curl.dll</title>
		<link>http://www.bradleyproctor.com/apache-php-windows-and-php_curl-dll/</link>
		<comments>http://www.bradleyproctor.com/apache-php-windows-and-php_curl-dll/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 16:26:19 +0000</pubDate>
		<dc:creator>Brad Proctor</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bradleyproctor.com/?p=1591</guid>
		<description><![CDATA[FYI: If you have the displeasure of having to work on a Windows server, and get this: PHP Warning: PHP Startup: Unable to load dynamic library 'C:\Program Files (x86)\PHP\ext\php_curl.dll' - %1 is not a valid Win32 application. in Unknown on line 0 Then try copying the libeay32.dll and ssleay32.dll files from your PHP installation and [...]]]></description>
			<content:encoded><![CDATA[<p><strong>FYI:</strong> If you have the displeasure of having to work on a Windows server, and get this:</p>
<pre>PHP Warning:  PHP Startup: Unable to load dynamic library 'C:\Program Files (x86)\PHP\ext\php_curl.dll' - %1 is not a valid Win32 application. in Unknown on line 0</pre>
<p>Then try copying the libeay32.dll and ssleay32.dll files from your PHP installation and put them into your Apache /bin directory.  Finally got it working after much unsuccessful googling and banging head against the wall.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bradleyproctor.com/apache-php-windows-and-php_curl-dll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Password Hashing Class v1.3</title>
		<link>http://www.bradleyproctor.com/password-hashing-class-v1-3/</link>
		<comments>http://www.bradleyproctor.com/password-hashing-class-v1-3/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 20:54:36 +0000</pubDate>
		<dc:creator>Brad Proctor</dc:creator>
				<category><![CDATA[Internet Security]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bradleyproctor.com/?p=1535</guid>
		<description><![CDATA[I'm pleased to present version 1.3 of my password hashing class. I've added support for PBKDF2 and cleaned up a few things in the code.  I'm using base64 to encode the hash rather than hex now, so this also drops the size of the derived hash from 144 characters down to 104, which makes it that much leaner for the database.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m pleased to present version 1.3 of my password hashing class. I&#8217;ve added support for PBKDF2 and cleaned up a few things in the code.  I&#8217;m using base64 to encode the hash rather than hex now, so this also drops the size of the derived hash from 144 characters down to 104, which makes it that much leaner for the database.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * password.php
 *
 * Copyright (c) 2010-2011 Brad Proctor. (http://bradleyproctor.com)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @author      Brad Proctor
 * @copyright   Copyright (c) 2010-2011 Brad Proctor
 * @license     MIT License (http://www.opensource.org/licenses/mit-license.php)
 * @link        http://bradleyproctor.com/
 * @package     framework
 * @since       1.0
 * @version     1.3
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * Required values:
 * AUTH_SALT: a unique site-wide value to compliment the unique salts
 * AUTH_LEVEL: Used for key stretching.  It is used to calculate the number of iterations to run the
 *    hashing algorithm. Raise this to increase security, lower this to make it run faster.  Default value
 *    is 5.
 */</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AUTH_SALT'</span><span style="color: #339933;">,</span>  <span style="color: #0000ff;">'RYt^|lqk1C#y;+yyPGg-TLAQ7hb1p%+hcU_dj|kY7yH!0*]+lwxkA=uU-0*~|2yi}PaEj,VYLP-hW#{|'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AUTH_LEVEL'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * The Password class works by generating a 104-character hash.  The first 16 characters are a unique
 * salt value that is generated for each password.  The rest of the 88 characters is the hash generated
 * by the whirlpool algorithm, which is much stronger than common md5 or sha1 methods.  The hash value is
 * also created using the HMAC method and a site wide key is used to further secure the hash.  The site
 * wide key can be any value, but a very strong 80-character unique value for AUTH_SALT can be generated at
 * http://bradleyproctor.com/tools/salt.php
 *
 * Usage:
 * $hash = Password::createPasswordHash('password');  // Store this value in your database
 *
 * if (Password::comparePassword('password', $hash) === true) {
 *    // Password is correct
 * } else {
 *    // Password was incorrect
 * }
 */</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> Password <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</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: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Generate a password salt
     *
     * @param int $length
     *    The number of characters that the salt should be
     *
     * @return
     *    Returns a salt that can be used to salt a password hash
     */</span>
    final static <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> createPasswordSalt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$salt</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'C'</span><span style="color: #339933;">,</span> <span style="color: #990000;">dechex</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">return</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</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;">/**
     *  PBKDF2 Implementation (described in RFC 2898)
     *  Password-Based Key Derivation Function
     *  (Simplified, since some variables are known)
     *
     *  @param string $p
     *      The plain text password
     *
     *  @param string $s
     *      The salt used to generate the hash
     *
     *  @return string
     *      Derived key
    */</span>
    final static <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> pbkdf2<span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #339933;">,</span> <span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$hl</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'whirlpool'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$ib</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash_hmac</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'whirlpool'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$s</span> <span style="color: #339933;">.</span> <span style="color: #990000;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'N'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> AUTH_LEVEL <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$ib</span> ^<span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash_hmac</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'whirlpool'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">.</span> AUTH_SALT<span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</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;">return</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ib</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;">/**
     * Generate a 104 character password hash
     *
     * @param $password
     *    The plain text password
     *
     * @param $salt
     *    The salt to use to generate the password
     *
     * @return
     *    Returns the 104-character hashed and salted password
     */</span>
    final static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createPasswordHash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$salt</span> or <span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">createPasswordSalt</span><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: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">pbkdf2</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</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;">/**
     * Compare a password with a hash
     *
     * @param $password
     *    The plain text password to compare
     *
     * @param $hash
     *    The 104 character password hash
     *
     * @return
     *    Returns TRUE if the password matches, FALSE if not
     */</span>
    final static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> comparePassword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$hash</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">createPasswordHash</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> AUTH_LEVEL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bradleyproctor.com/password-hashing-class-v1-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming and Politics</title>
		<link>http://www.bradleyproctor.com/programming-and-politics/</link>
		<comments>http://www.bradleyproctor.com/programming-and-politics/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 21:51:33 +0000</pubDate>
		<dc:creator>Brad Proctor</dc:creator>
				<category><![CDATA[Politics]]></category>

		<guid isPermaLink="false">http://www.bradleyproctor.com/?p=1585</guid>
		<description><![CDATA[If you&#8217;re a developer, you&#8217;ll probably be able to relate to this analogy.  Once upon a time there was a software project that you were involved that you loved and those that used it loved because it did it&#8217;s job well.  Then over time new features got added to it to expand it&#8217;s capabilities.  It [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re a developer, you&#8217;ll probably be able to relate to this analogy.  Once upon a time there was a software project that you were involved that you loved and those that used it loved because it did it&#8217;s job well.  Then over time new features got added to it to expand it&#8217;s capabilities.  It was modified in different ways to fit different needs.  As new technologies emerged, some where incorporated, but others could not be because the system was already established.  As time went on, the project became outdated, slow, and inefficient.  The code was difficult to maintain and either one of two things happened  Either the project was abandoned in favor of something newer and better or there was a complete rewrite which made the software relavent again.</p>
<p>This is where I feel we are with our current government.  It has become slow and inefficient.  It is difficult to manage and does not meet the needs of the people.  Most everyone agrees that our government is broken regardless of their political opinions.  I hear often that the solution is to vote the current people out of office.  But I don&#8217;t believe this solves anything because the problem is with the system as a whole.  It doesn&#8217;t matter how good willed and motivated of a person we put in office, they aren&#8217;t going to be able to make a difference.</p>
<p>Take President Obama for example.  Whether you agree with him politically or not, you pretty much have to agree that he was highly motivated in 2008 to make a difference.  Did he make a difference?  Not much.  However, I don&#8217;t believe it was for a lack of trying.  It&#8217;s just not possible with the current system of government that we have in this country.</p>
<p>We vote the republicans into office, nothing happens, so we get angry and vote the democrats in, nothing happens.  We get angry again and vote the republicans in and we just keep continuing the cycle.</p>
<p>I think it is time for a rewrite.  We have learned a lot from our current government, and from other governments.  We have seen what is successful and what is not.  The economics and philosophies have change a lot over the course of our countries history and we need a government designed for it.  I believe that with a proper democratic process, we could write a new system of government that would be much more effective and relevant.</p>
<p>Let&#8217;s rewrite our government!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bradleyproctor.com/programming-and-politics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Support the Occupy Wall Street Movement</title>
		<link>http://www.bradleyproctor.com/support-the-occupy-wall-street-movement/</link>
		<comments>http://www.bradleyproctor.com/support-the-occupy-wall-street-movement/#comments</comments>
		<pubDate>Sun, 02 Oct 2011 16:46:07 +0000</pubDate>
		<dc:creator>Brad Proctor</dc:creator>
				<category><![CDATA[Politics]]></category>

		<guid isPermaLink="false">http://www.bradleyproctor.com/?p=1581</guid>
		<description><![CDATA[http://www.occupytogether.org/ https://occupywallst.org/ https://www.facebook.com/OccupyWallSt‎ &#8220;I am a most unhappy man. I have unwittingly ruined my country. A great industrial nation is controlled by its system of credit. Our system of credit is concentrated. The growth of the nation, therefore, and all our activities are in the hands of a few men. We have come to be [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://occupytogether.org/">http://www.occupytogether.org/</a></p>
<p><a href="https://occupywallst.org/">https://occupywallst.org/</a></p>
<p><a href="https://www.facebook.com/OccupyWallSt‎">https://www.facebook.com/OccupyWallSt‎</a></p>
<p>&#8220;I am a most unhappy man. I have unwittingly ruined my country. A great industrial nation is controlled by its system of credit. Our system of credit is concentrated. The growth of the nation, therefore, and all our activities are in the hands of a few men. We have come to be one of the worst ruled, one of the most completely controlled and dominated Governments in the civilized world no longer a Government by free opinion, no longer a Government by conviction and the vote of the majority, but a Government by the opinion and duress of a small group of dominant men.&#8221;</p>
<p><em>-Woodrow Wilson, after signing the Federal Reserve into existence</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bradleyproctor.com/support-the-occupy-wall-street-movement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to keep nbproject out of your subversion repository</title>
		<link>http://www.bradleyproctor.com/how-to-keep-nbproject-out-of-your-subversion-repository/</link>
		<comments>http://www.bradleyproctor.com/how-to-keep-nbproject-out-of-your-subversion-repository/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 23:45:00 +0000</pubDate>
		<dc:creator>Brad Proctor</dc:creator>
				<category><![CDATA[Web Programming]]></category>

		<guid isPermaLink="false">http://www.bradleyproctor.com/?p=1578</guid>
		<description><![CDATA[Netbeans users, if your using netbeans with SVN, you may have had the problem that the nbproject directory tries to get included into your subversion repository.  This is fine if you are the only developer, but it's best to just keep that folder from getting included in your repository.]]></description>
			<content:encoded><![CDATA[<p>Netbeans users, if your using netbeans with SVN, you may have had the problem that the nbproject directory tries to get included into your subversion repository.  This is fine if you are the only developer, but it&#8217;s best to just keep that folder from getting included in your repository.</p>
<p>The fix is pretty simple, just add this line to your subversion config file</p>
<p>global-ignores = *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo *.rej *~ #*# .#* .*.swp .DS_Store nbproject</p>
<p>On Windows:<br />
C:\Users\[your username]\.netbeans\7.0\config\svn\config\config</p>
<p>On Mac/Linux:<br />
/home/[username]/.svn/config</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bradleyproctor.com/how-to-keep-nbproject-out-of-your-subversion-repository/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Our New Deck</title>
		<link>http://www.bradleyproctor.com/our-new-deck/</link>
		<comments>http://www.bradleyproctor.com/our-new-deck/#comments</comments>
		<pubDate>Wed, 25 May 2011 01:14:27 +0000</pubDate>
		<dc:creator>Brad Proctor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.bradleyproctor.com/?p=1503</guid>
		<description><![CDATA[A photo tour of the building process of our deck
<a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/54583.jpg"><img class="size-medium wp-image-1559" title="54583" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/54583-300x200.jpg" alt="" width="300" height="200" /></a>]]></description>
			<content:encoded><![CDATA[<p>After pouring the concrete we covered it with plastic over night to let the concrete cure. The plastic helps it from curing too fast.</p>
<div id="attachment_1504" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53950.jpg"><img class="size-medium wp-image-1504" title="Deck Footers" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53950-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Waiting for footings to dry</p></div>
<p>After 24 hours the footers are ready to build on&#8230;</p>
<div id="attachment_1505" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53952.jpg"><img class="size-medium wp-image-1505" title="Deck Footings" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53952-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">The finished footings</p></div>
<div id="attachment_1506" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53955.jpg"><img class="size-medium wp-image-1506" title="Footers" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53955-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Close up of a footer</p></div>
<p>&nbsp;</p>
<div id="attachment_1507" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53957.jpg"><img class="size-medium wp-image-1507" title="Posts" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53957-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Posts places on footers</p></div>
<p>&nbsp;</p>
<div id="attachment_1508" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53959.jpg"><img class="size-medium wp-image-1508" title="Posts" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53959-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Notches for beams cut into posts</p></div>
<p>&nbsp;</p>
<div id="attachment_1509" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53961.jpg"><img class="size-medium wp-image-1509" title="Posts" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53961-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Another view of the completed posts</p></div>
<p>&nbsp;</p>
<div id="attachment_1510" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53962.jpg"><img class="size-medium wp-image-1510" title="Beams" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53962-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Beams bolted to posts</p></div>
<p>&nbsp;</p>
<div id="attachment_1512" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53967.jpg"><img class="size-medium wp-image-1512" title="Joists" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53967-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Hanging the joists</p></div>
<div id="attachment_1511" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53966.jpg"><img class="size-medium wp-image-1511" title="Beams" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53966-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Another view of the completed beams</p></div>
<p>&nbsp;</p>
<div id="attachment_1514" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53971.jpg"><img class="size-medium wp-image-1514" title="Joists" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53971-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Another view of the joists being installed</p></div>
<p>&nbsp;</p>
<div id="attachment_1518" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53983.jpg"><img class="size-medium wp-image-1518" title="Decking" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53983-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Installing the deck boards</p></div>
<p>&nbsp;</p>
<div id="attachment_1515" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53973.jpg"><img class="size-medium wp-image-1515" title="Joists" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53973-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Completed joists</p></div>
<p>&nbsp;</p>
<div id="attachment_1516" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53974.jpg"><img class="size-medium wp-image-1516" title="Gate" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53974-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Installing the gate</p></div>
<p>&nbsp;</p>
<div id="attachment_1517" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53979.jpg"><img class="size-medium wp-image-1517" title="Joists" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53979-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Another view of the completed joists</p></div>
<p>&nbsp;</p>
<div id="attachment_1557" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53988.jpg"><img class="size-medium wp-image-1557" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/53988-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Putting the deck boards down</p></div>
<p>&nbsp;</p>
<div id="attachment_1559" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/54583.jpg"><img class="size-medium wp-image-1559" title="54583" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/54583-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">The finished deck</p></div>
<p>&nbsp;</p>
<div id="attachment_1562" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.bradleyproctor.com/wp-content/uploads/2011/05/54588.jpg"><img class="size-medium wp-image-1562" title="54588" src="http://www.bradleyproctor.com/wp-content/uploads/2011/05/54588-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Finished Deck</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.bradleyproctor.com/our-new-deck/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A simple system for URI routing in PHP</title>
		<link>http://www.bradleyproctor.com/a-simple-system-for-uri-routing-in-php/</link>
		<comments>http://www.bradleyproctor.com/a-simple-system-for-uri-routing-in-php/#comments</comments>
		<pubDate>Fri, 11 Mar 2011 19:00:36 +0000</pubDate>
		<dc:creator>Brad Proctor</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bradleyproctor.com/?p=1492</guid>
		<description><![CDATA[For simpler websites, building on top of a framework can be overkill.  But if you still want clean URLs and the ability to write your own controllers, you'll need some type of system to keep things in order.  The system presented below is a very small dispatcher that parses your URIs and passes control to your controller.]]></description>
			<content:encoded><![CDATA[<p>For simpler websites, building on top of a framework can be overkill.  But if you still want clean URLs and the ability to write your own controllers, you&#8217;ll need some type of system to keep things in order.  The system presented below is a very small dispatcher that parses your URIs and passes control to your controller.</p>
<p>Here is an example of the configuration.  The first array specifies fixed routes (routes without any regex in them).  This is separated from the regex array for simply for speed.  Each element in the array represents a route.  The key is the URI and the array that it points to has the controller and the method to call for that route.  Any slashes before and after the URI are removed when searching for a match, so you don&#8217;t need to put those in your routes.  Also, an empty key represents the index</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'routes'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fixed'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">''</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SupportPages'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'contact'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SupportPages'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'contact'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'support/help'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SupportPages'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'help'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The regex array has the same format, except that you can put regular expressions in for the routes.  This is a lot more complex and requires that you be fairly familiar with regular expressions.  Anything that you put in parenthesis will be converted into a parameter to the method that you call, so in the example below, the SupportPages class with the test method would be called and the number from the URI would be passed as the first (and in this example, the only) parameter. Also, if you omit the method in this array, it will try to use the first match in the regex as the name of the method to call.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'routes'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'regex'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'/^test\/([0-9]+)$/'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SupportPages'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>To use the code, you will need to set up your web server to send all requests to index.php and then put the code below into your index.php file.  On line 46, the file that contains the code for your control is simply included.  This can be modified to whatever your file structure happens to be.  You could remove this line altogether and create an __autoload function.</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Make sure we have a clean uri to work with</span>
<span style="color: #000088;">$uri</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index.php'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Check for a fixed route</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'routes'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fixed'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$uri</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$route</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'routes'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fixed'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$uri</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$route</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$route</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</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;">// No match, check if we have a match in the predefined regex routes</span>
   <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'routes'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'regex'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$route</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: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$uri</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>        
&nbsp;
         <span style="color: #666666; font-style: italic;">// Found a match, set the controller</span>
         <span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$route</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">// Set the action, if the action is not specified, then it is</span>
         <span style="color: #666666; font-style: italic;">// set to the first match</span>
         <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #990000;">isset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$route</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$uri</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #990000;">isset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'index'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</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: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$route</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
&nbsp;
         <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// There were params found, so dump the first value in matches.</span>
            <span style="color: #666666; font-style: italic;">// The rest of $matches contains the parameters.</span>
            <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
         <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$controller</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Status: 404 Not Found'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">require</span> <span style="color: #000088;">$controller</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$controller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #000088;">$action</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For completeness, here is a really simple controller to see how one might be structured.  To work with the sample code above, this file would be named SupportPages.php and would be stored in the same directory as index.php.  Again, you could make this into a much more robust system by modifying line 46 to match your prefered directory structure.</p>

<div class="wp_syntax"><table><tr><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"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> SupportPages <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> contact<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'hello'</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> test<span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The Param is: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$param</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bradleyproctor.com/a-simple-system-for-uri-routing-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding huge numbers with PHP</title>
		<link>http://www.bradleyproctor.com/adding-huge-numbers-with-php/</link>
		<comments>http://www.bradleyproctor.com/adding-huge-numbers-with-php/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 21:13:17 +0000</pubDate>
		<dc:creator>Brad Proctor</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bradleyproctor.com/?p=1484</guid>
		<description><![CDATA[Ever have the need to add some really large integers.  Large enough that they don't fit in the standard data types.  No?  Didn't think so.  But just in case you do, here is a function that can add two numbers no matter how large.  This function returns a string with the resulting number in it.  One caveat: It only works with positive integers.]]></description>
			<content:encoded><![CDATA[<p>Ever have the need to add some really large integers.  Large enough that they don&#8217;t fit in the standard data types.  No?  Didn&#8217;t think so.  But just in case you do, here is a function that can add two numbers no matter how large.  This function returns a string with the resulting number in it.  One caveat: It only works with positive integers.</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> badd<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$y</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$length_x</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$length_y</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// Make sure that $x is the longer one, swap if necessary</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$length_x</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$length_y</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$length_x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$length_x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$length_y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$length_y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000088;">$rx</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrev</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$ry</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrev</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// Add up the digits</span>
   <span style="color: #000088;">$sum</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$length_x</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$sum</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ry</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$rx</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$ry</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$rx</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// Don't need these anymore, free the memory</span>
   <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rx</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// Take care of carrying digits</span>
   <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sum</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</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;">$sum</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #339933;">++</span><span style="color: #000088;">$sum</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$sum</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// Concatenate the digits to make the final answer</span>
   <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sum</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$result</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$sum</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bradleyproctor.com/adding-huge-numbers-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A simple and very secure password class for php</title>
		<link>http://www.bradleyproctor.com/a-simple-and-very-secure-password-class-for-php/</link>
		<comments>http://www.bradleyproctor.com/a-simple-and-very-secure-password-class-for-php/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 17:15:45 +0000</pubDate>
		<dc:creator>Brad Proctor</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bradleyproctor.com/?p=1433</guid>
		<description><![CDATA[I've made some more changes to my password class. This time I've added stretching, which is basically just running the hash function multiple times to slow down the code. Usage is very simple, as shown in the comments in the code. Also, you need to define AUTH_SALT and AUTH_LEVEL somewhere in your code. A unique value for AUTH_SALT can be found here: http://bradleyproctor.com/tools/salt.php.  Setting AUTH_LEVEL to 10 will give a good balance between security and speed.  Full source below:]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve made some more changes to my password class.  This time I&#8217;ve added stretching, which is basically just running the hash function multiple times to slow down the code.  Usage is very simple, as shown in the comments in the code.  Also, you need to define AUTH_SALT and AUTH_LEVEL somewhere in your code.  A unique value for AUTH_SALT can be found here: <a href="http://bradleyproctor.com/tools/salt.php">http://bradleyproctor.com/tools/salt.php</a></p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Password.class.php
 *
 * Copyright (c) 2010-2011 Brad Proctor. (http://bradleyproctor.com)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @author      Brad Proctor &lt;brad@bradleyproctor.com&gt;
 * @copyright   Copyright (c) 2010-2011 Brad Proctor
 * @license     MIT License (http://www.opensource.org/licenses/mit-license.php)
 * @link        http://bradleyproctor.com/
 * @package     framework
 * @since       1.0
 * @version     1.2
 */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * The Password class works by generating a 144-character hash.  The first 16 characters are a unique
 * salt value that is generated for each password.  The rest of the 128 characters is the hash generated
 * by the whirlpool algorithm, which is much stronger than common md5 or sha1 methods.  The hash value is
 * also created using the HMAC method and a site wide key is used to further secure the hash.  The site
 * wide key can be any value, but a very strong 80-character unique value for AUTH_SALT can be generated at
 * http://bradleyproctor.com/tools/salt.php
 *
 * Required values:
 * AUTH_SALT: a unique site-wide value to compliment the unique salts
 * AUTH_LEVEL: Used for key stretching.  It is used to calculate the number of iterations to run the
 *    hashing algorithm. Raise this to increase security, lower this to make it run faster.  Default value
 *    is 10.
 *
 * Usage:
 * $hash = Password::createPasswordHash('password');  // Store this value in your database
 *
 * if (Password::comparePassword('password', $hash) === true) {
 *    // Password is correct
 * } else {
 *    // Password was incorrect
 * }
 */</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> Password <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Do not allow the object to be constructed
     */</span>
    <span style="color: #000000; font-weight: bold;">private</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: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Generate a password salt
     *
     * @return
     *    Returns a salt that can be used to salt a password hash
     */</span>
    final static <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> createPasswordSalt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$salt</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">dechex</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mt_rand</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;">return</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</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;">/**
     * Generate a 144 character password hash
     *
     * @param $password
     *    The plain text password
     *
     * @param $salt
     *    The salt to use to generate the password
     *
     * @return
     *    Returns the 144-character hashed and salted password
     */</span>
    final static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createPasswordHash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$salt</span> or <span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">createPasswordSalt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #990000;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> AUTH_LEVEL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">do</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">hash_hmac</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'whirlpool'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> AUTH_SALT<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;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">--</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$hash</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Compare a password with a hash
     *
     * @param $password
     *    The plain text password to compare
     *
     * @param $hash
     *    The 144 character password hash
     *
     * @return
     *    Returns TRUE if the password matches, FALSE if not
     */</span>
    final static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> comparePassword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$hash</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">createPasswordHash</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> AUTH_LEVEL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bradleyproctor.com/a-simple-and-very-secure-password-class-for-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Optimization Tips</title>
		<link>http://www.bradleyproctor.com/php-optimization-tips/</link>
		<comments>http://www.bradleyproctor.com/php-optimization-tips/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 04:23:34 +0000</pubDate>
		<dc:creator>Brad Proctor</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bradleyproctor.com/?p=1406</guid>
		<description><![CDATA[<strong>1. echo vs print</strong>

The <em>echo</em> and <em>print</em> methods are virtually the same.  They are both built into the language so they are faster than function calls.  The one thing that makes them different is that <em>print</em> returns whether it was successful or not while <em>echo</em> does not return anything.  This puts a little overhead on <em>print</em> that <em>echo</em> does not have.  So unless you need the return value, stick with <em>echo</em>.

<strong>2. Single quotes vs double quotes</strong>

Double quotes has the ability of embedding variables within the string and also translating other escape characters.  With single quotes, what you see is what you get.  The string is exactly as you typed it.  Because PHP has to do extra work in checking a double quote string, using single quotes is faster.
]]></description>
			<content:encoded><![CDATA[<p><strong>1. echo vs print</strong></p>
<p>The <em>echo</em> and <em>print</em> methods are virtually the same.  They are both built into the language so they are faster than function calls.  The one thing that makes them different is that <em>print</em> returns whether it was successful or not while <em>echo</em> does not return anything.  This puts a little overhead on <em>print</em> that <em>echo</em> does not have.  So unless you need the return value, stick with <em>echo</em>.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Testing'</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'Testing'</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p><strong>2. Single quotes vs double quotes</strong></p>
<p>Double quotes has the ability of embedding variables within the string and also translating other escape characters.  With single quotes, what you see is what you get.  The string is exactly as you typed it.  Because PHP has to do extra work in checking a double quote string, using single quotes is faster.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Testing'</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Testing&quot;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p><strong>3. Use commas for multiple parameters</strong></p>
<p>So at this point your using echo, and your putting your strings in single quotes.  But what do you do when you need to echo a variable, since we aren&#8217;t using double quotes, we need to use . to concatenate it right?  wrong.  PHP has a built in operator that is not well known but a comma can be used to specific multiple parameters to the echo command in much the same way that you would specify multiple parameters to a function.  This is much faster than string concatenation with the period operator.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You are '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$percent</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'% correct'</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You are '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$percent</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'% correct'</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p><strong>4. Use lowercase reserved words</strong></p>
<p>You probably already do this for the most part, but because PHP is case insensitive to keywords you can write code with reserved words that are in capital letters.  Words such as echo, class, foreach, can all be written in capital letters. For example, ECHO, CLASS, and FOREACH will all work just fine.  Because PHP stores these words internally in lowercase, it is fastest to use them in lowercase.</p>
<p>While almost everyone already does this, there are some cases where you may not.  The keywords true and false are faster than using TRUE and FALSE.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Testing'</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">ECHO</span> <span style="color: #0000ff;">'Testing'</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<h2>5. strtr vs str_replace vs preg_replace</h2>
<p>The three of these function have their place and are all useful for difference circumstances.  strtr is the simplest of the functions and is also the quickest.  While it has the capability of handling multiple characters, don&#8217;t use it for that, use str_replace instead.  strtr is fastest for single character replacement.  str_replace fastest for multiple character replacement.  And only use preg_replace if you really need regular expressions.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">strtr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'b'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">strtr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'abc'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'def'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'c'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'e'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'f'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'one'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'apple'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'two'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'orange'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">strtr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'one'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'two'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'apple'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'orange'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p><strong>6. require() vs require_once()</strong></p>
<p>Use you really need require_once(), you should always be using require().  If you do find yourself using require_once often, you should look into why that is, because in most cases a well structured program should not need require_once.</p>
<p><strong>7. Use static methods in your classes</strong></p>
<p>If you are writing object oriented code, check your function to see if they can be static methods and if so declare them static.  Calling static methods are significantly faster than calling regular methods because PHP has a lot less bookkeeping to worry about with a static method.</p>
<p><strong>8. Pre increment/decrement instead of post</strong></p>
<p>It is faster to use ++$count; than it is to use $count++;  This is because of the internal workings of PHP.  When a value is pre-incremented or decremented the change happens to the variable but when it is post-incremented or decremented, a temporary variable is created the value is incremented or decremented and then copied back into the original variable.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">++</span><span style="color: #000088;">$count</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$count</span><span style="color: #339933;">++;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p><strong>9.pathinfo() vs basename()</strong></p>
<p>The pathinfo function comes with some options that can be used to get information about a path.  If you use these options, chances are there is a better way.  PATHINFO_BASENAME returns the base name of the path, but so does basename() and it is 7% faster than using pathinfo.  The same applies to using PATHINFO_DIRNAME.  Use the dirname() function instead.  dirname() is 53% faster than pathinfo.<br />
It&#8217;s also worth noting that finding the directory name is nearly 10 times faster than finding the basename of a path.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">pathinfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">,</span> PATHINFO_BASENAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">pathinfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">,</span> PATHINFO_DIRNAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p><strong>10. Finding the application root directory</strong></p>
<p>A common task in a PHP application is finding the root directory that the application sits on top of.  This is useful for two reasons.  First, it can be used as a reference to finding all other files, particularly for require and include statements.  Also, using the full path instead of relative paths requires less overhead on the part of the operating system.</p>
<p>The best way to find the root directory is to use dirname(__FILE__), or if you are using PHP 5.3 you can use __DIR__ and save yourself an extra function call.  This will give you the directory portion of the current file.  You can then use this to find any other file in your applications hierarchy.</p>
<p>Another method is to use $_SERVER['DOCUMENT_ROOT']; While this in theory should work, and usually does, a poorly configured web server can have this pointing somewhere else then where you would expect.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span>
__DIR__</pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'DOCUMENT_ROOT'</span><span style="color: #009900;">&#93;</span></pre></div></div>

</td>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROOT'</span><span style="color: #339933;">,</span> __DIR__<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span>ROOT <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/include/functions.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'include/functions.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p><strong>11. Don&#8217;t use error suppression</strong></p>
<p>The @ character can be used in front of a statement to suppress errors, however it is advised not to use this for a couple of reasons.  First, it&#8217;s a hack.  Errors are there to tell you that something is wrong with your code, simple ignoring it is not a fix.  Second, it is slow.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p><strong>12. Multiple isset</strong></p>
<p>If you need to check to see if multiple variables are set, you can do so with a single isset call rather than multiple.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #990000;">isset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #990000;">isset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p><strong>13. switch case vs if elseif</strong></p>
<p>If you have several else if statements in your code, it may be possible to covert this into a switch .. case statement.  Using a switch .. case statement is typically faster.</p>
<p><strong>14. Avoid function calls within loops</strong></p>
<p>This applies to any language, but PHP, as an interpreted language is even more susceptible to this than other languages because compiled languages like C and C++ are able to do a bit of optimizing to move stuff outside of the loop for you.  So whenever possible move whatever you can outside of the loop.  Sometimes this is less obvious like the example below.</p>
<table>
<tbody>
<tr>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td style="background: #efe;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span></pre></div></div>

</td>
<td style="background: #fee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p><strong>15. Derived classes vs base classes</strong></p>
<p>Calling a method in a derived class is faster than calling one in the base class.  This is because PHP first looks for the function in the top most class and works it&#8217;s way down.  So a method in the base class will be the last place it looks before giving up and the derived class will be the first place it looks.</p>
<p>While it would be a very bad idea to structure your classes based on this bit of information, I do believe it is worth noting and being aware of when writing your code.</p>
<p><strong>16. call_user_func_array</strong></p>
<p>The call_user_func_array() function is fairly slow, but if you know how many arguments your function accepts, you can call it much faster without call_user_func_array.  Even if you don&#8217;t, you can nearly double it&#8217;s speed with this switch .. case statement:<br />
(where $c = the class you are calling, $m = the method, and $p is an array of the parameters to call the function with)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span> <span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> <span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span> <span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span> <span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">:</span> <span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #000088;">$m</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>17. Use arrays instead of objects when appropriate</strong></p>
<p>Sometimes we can go too far in our endeavor to make everything object oriented.  OOP has a cost, and if your data is able to be stored as an array do it.  Don&#8217;t create an object out of it just for the sake of object oriented programming.  Objects carry a lot of overhead with them that arrays do not have.  So if all you need is an array, don&#8217;t make it an object for no reason.</p>
<p><strong>18. isset instead of strlen</strong></p>
<p>I don&#8217;t personally care for this optimization as I think it makes the code not very readable.  But the idea is that if you need to check to see if a string is a certain length, you can do so quicker with isset than with strlen.  So, for example, if you needed to check to see if a string was shorter than 10 characters, you could do so with</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>would return false if the string was shorter than 10 characters.</p>
<p><strong>19. unset arrays</strong></p>
<p>If you are using arrays in your code, you can sometimes save some memory by using unset() to free the memory when it is no longer needed.  This isn&#8217;t needed as often as it may appear at first glance.  When trying to determine whether to unset a variable, keep in mind that when your function exists, any local variables will be freed anyways, so you usually only need to do this when you are using an array for a temporary time, then are allocated more memory right after that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bradleyproctor.com/php-optimization-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

