<?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>gigaspartan</title>
	<atom:link href="http://gigaspartan.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gigaspartan.com</link>
	<description>You source for web news and answers.</description>
	<lastBuildDate>Thu, 03 Feb 2011 06:32:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Save The Kittens! PHP Must Follows!</title>
		<link>http://gigaspartan.com/2011/02/01/save-the-kittens-php-must-follows/</link>
		<comments>http://gigaspartan.com/2011/02/01/save-the-kittens-php-must-follows/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 06:13:09 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[Posts]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=173</guid>
		<description><![CDATA[There’s an old adage – dating back to the 1700s – which, in English, says: “A kitten dies each time a PHP programmer doesn’t follow best practices.” Okay, not really; but just go with it! Getting started in PHP can be a daunting experience. With that in mind, these 20 tips will teach you how [...]]]></description>
			<content:encoded><![CDATA[<p>There’s an old adage – dating back to the 1700s – which, in English, says: “<em>A kitten dies each time a PHP programmer doesn’t follow best practices.</em>” Okay, not really; but just go with it! </p>
<p> Getting started in PHP can be a daunting experience. With that in mind, these 20 tips will teach you how to follow best practices, and save lives…kitty lives. </p>
<p><span id="more-17889"></span></p>
<p></p>
<h2><span>0.</span> Program as Often as You Possibly Can</h2>
<blockquote class="pullquote">
<p> Programming often and with purpose will make the lessons you learn stick.</p>
</blockquote>
<p> Did you study a foreign language in school? Studied all the parts of speech, learned the verbs and how to conjugate them, followed along as your teacher said common phrases? </p>
<p> How much of that language do you still speak? </p>
<p> If you’re answer is, “<em>none</em>,” I’m willing to bet it’s due to the fact that you never actually <em>used</em> the language — you only studied it. But if you can still hold up a conversation, it’s likely because you actually spent some time speaking that language outside of the learning environment. Perhaps you spent a year abroad, or worked a job where a second language was necessary? </p>
<p> Whatever the reason, you retained it because you used it in real-life situations and <strong>put it into a personal context that is much easier to recall later.</strong> </p>
<blockquote>
<p> PHP is a foreign language, just like Spanish or French. In order to become comfortable with it, you need to use it outside of the classroom setting. Tutorials and sample projects are great for teaching the fundamentals, but unless you’re applying those concepts to your own projects, it will be much more difficult to apply those fundamentals in context and burn them into your memory. </p>
</blockquote>
<p> So, don’t worry that you “don’t know enough” to build a project. When you choose your project, you have a valid reason to research and implement a concept. Programming often and with purpose will make the lessons you learn stick. </p>
<p><span id="more-173"></span><br />
</p>
<h2><span>1.</span> Get Familiar with the PHP Manual</h2>
<p> Every list of tips for beginners has this tip, and for good reason.
</p>
<blockquote><p>Learning to navigate the PHP documentation is the single most useful thing you can do for yourself as a programmer. </p>
</blockquote>
<p> If you look in my browser history at the sites I most often visit, <a href="http://www.php.net/manual/en/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','www.php.net']);">the PHP manual</a> will be right at the top. I don’t suspect that will change for as long as PHP remains my programming language of choice. </p>
<p> At first, the manual does look rather daunting — it doesn’t seem to be particularly easy to browse, and the navigation can be a bit awkward at times. However, you’ll get the hang of it quickly. </p>
<p> Perhaps the best thing to know about the PHP manual is that <strong>most functions can be looked up using the pattern <code>http://php.net/<em>function-name</em></code></strong> in your address bar. For example, to look up the <a href="http://php.net/strpos" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>strpos()</code></a> function, use http://php.net/strpos, and for <a href="http://php.net/array-key-exists" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>array_key_exists()</code></a>, use http://php.net/array-key-exists. (NOTE: pay attention to the omission of parentheses and the substitution of hyphens (<code>-</code>) for the underscore (<code>_</code>) in the address.) </p>
<h3>1a. Read the Comments!</h3>
<p> It&#8217;s easy to overlook the comments, but do yourself a favor and have a look through them. If you&#8217;re getting an unexpected result from a function, <strong>chances are someone has spotted it and explained it in the comments.</strong> </p>
<p> You can also pick up a plethora of great tips and ideas from the developer community by reading through comments. </p>
<p></p>
<h2><span>2.</span> Take Advantage of the Huge Online PHP Community</h2>
<p> In addition to the PHP manual, there are wonderful developer communities all over the internet. Some of my personal favorites include <a href="http://stackoverflow.com/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','stackoverflow.com']);">StackOverflow.com</a> and the <a href="http://w3schools.invisionzone.com/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','w3schools.invisionzone.com']);">W3Schools.com forum</a>. </p>
<p> Additionally, Twitter is a surprisingly excellent place to post PHP questions. If you tag a tweet with <a href="http://twitter.com/search#search?q=%23php" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','twitter.com']);">#PHP</a>, it&#8217;s likely someone in the community will spot it and lend a hand. </p>
<p> <em>A Note About Twitter:</em> Of course, anything that&#8217;s useful will inevitably be overrun with spammers and those sorry individuals who deeply misunderstood the purpose of social media. If you&#8217;re going to use Twitter as a support network, you&#8217;ll probably want to routinely block or hide the accounts which spew job postings or retweet everything that mentions PHP. </p>
<blockquote>
<p> Just remember: as you get better, please try to <strong>pay it forward.</strong> The development community needs everyone to pitch in, and it won&#8217;t be long before you&#8217;ll have the ability to answer questions for other beginners. Don&#8217;t turn a deaf ear. </p>
</blockquote>
<p></p>
<h2><span>3.</span> Don&#8217;t Put Off Best Practices for Later</h2>
<p> As you&#8217;re learning, you&#8217;re going to hear a lot about &#8220;best practices&#8221; in programming; stuff like prepared statements and PEAR coding standards. </p>
<p> Do not put off learning this stuff because it seems hard.</p>
<blockquote>
<p> If something is a best practice, it&#8217;s not because we (meaning other PHP developers) got together and said, &#8220;How can we make life harder for the noobs?&#8221; </p>
</blockquote>
<p> Best practices exist to keep your scripts secure, fast, and manageable. Learn them as early as you can. In fact, don&#8217;t even bother learning the wrong way. </p>
<p> It takes just about the exact same amount of learning to figure out <code>mysql_query()</code> as it does to learn <a href="http://php.net/pdo" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);">PDO</a> or <a href="http://php.net/mysqli" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);">MySQLi</a>. So if you start with your choice of the latter two, you&#8217;re starting with a strong foundation in database interaction and, really, you&#8217;ve put in less overall effort. </p>
<p></p>
<h2><span>4.</span> Don&#8217;t Put Off Best Practices for Later!</h2>
<p> I just wanted to make sure you saw this. </p>
<blockquote>
<p> Seriously, folks. <strong>Don&#8217;t take shortcuts.</strong> Every time you violate best practices because the right way seems &#8220;too hard,&#8221; BP dips a kitten in crude oil. </p>
</blockquote>
<p> So if you won&#8217;t do it for yourself, your projects, your peers, or the advancement of the community at large, <strong>at least consider the kittens.</strong> </p>
<p></p>
<h2><span>5.</span> Make Code Self-Documenting</h2>
<blockquote class="pullquote">
<p> If you need to squeeze characters off your variable names to shave .2ms off your script’s execution time, there’s likely a whole different problem going on.</p>
</blockquote>
<p> It&#8217;s tempting, early on, to be &#8220;clever&#8221; with your variable and function names. Maybe you read an article about performance, or saw a code snippet that accomplished a ton of work in two lines of code. Maybe you want to create your own &#8220;signature style&#8221; of coding. Maybe you just heard that I hate it and you wanted to piss me off. </p>
<p> Whatever your temptation, <strong>resist it at all costs.</strong> </p>
<p> Consider the following snippet of code: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code13'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17313"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p173code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
 <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> b<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jason.lengstorf@copterlabs.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$d</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>
&nbsp;
 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The email address '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' belongs to the domain '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$d</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">function</span> b<span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> Does that make any sense to you? </p>
<p> Of course, you <em>can</em> figure out what it does, but why force anyone trying to work in your code to spend the extra 1-5 minutes scratching his head, trying to remember what $c is storing. </p>
<p> So let&#8217;s take that code and make it self-documenting: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code14'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17314"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p173code14"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
 <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> sanitize_string<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'support@google.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$email_pieces</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$domain</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$email_pieces</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The email address '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' belongs to the domain '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$domain</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">function</span> sanitize_string<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> There. Much better. Now, just by glancing at the code, you can get the general idea of what&#8217;s going on. No head-scratching, no muttered curses, and most importantly, <strong>no real difference.</strong> </p>
<p> Sure, you save a few bytes with short variable names. But, honestly, if you need to squeeze characters off your variable names to shave .2ms off your script&#8217;s execution time, there&#8217;s likely a whole different problem going on. </p>
<p></p>
<h2><span>6.</span> Add a Comment to Anything You Had to Think About</h2>
<blockquote class="pullquote">
<p>Comments are the sign of a competent programmer.</p>
</blockquote>
<p> Comments are not the sign of a novice. In fact, as I see more and more code that&#8217;s not mine, I&#8217;m starting to think that comments are the sign of a competent programmer, as they seem to be the only ones doing it. </p>
<p> If your code is self-documenting, you won&#8217;t require too many comments. However, no matter how clear your variable and function names are, you&#8217;ll always have spots where the action taken simply isn&#8217;t that obvious.</p>
<p> When that happens, slap a comment in there. &#8220;Future You&#8221; will give &#8220;Present You&#8221; a high five when the time comes to update the script. </p>
<blockquote>
<p> As a rule of thumb, <em>if you had to stop and think for a few seconds about what needed to happen to make the script work properly, it&#8217;s probably a good spot for a quick note.</em> </p>
</blockquote>
<p> Consider the following: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code15'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17315"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p173code15"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pieces</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_pop"><span style="color: #990000;">array_pop</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p> What does that do? Did you have to stop and think about it? Do you still not know for sure what&#8217;s stored in $extension? </p>
<p> Look at that snippet again, but with one quick comment: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code16'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17316"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p173code16"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Get the extension off the image filename</span>
<span style="color: #000088;">$pieces</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_pop"><span style="color: #990000;">array_pop</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p> Now, even if you don&#8217;t know <em>how or why that code works,</em> you at least know that <code>$extension</code> refers specifically to an image extension. If that saves &#8220;Future You&#8221; or another developer five seconds of processing the script&#8217;s intent, <strong>it was well worth your ten seconds of effort to add the comment in the first place.</strong> </p>
<p> As with most things, moderation is key. Too few comments and you risk leaving the next developer (or Future You) puzzled by a code snippet, which can even lead to accidental breaking of code because the solution, without explanation, might look silly or superfluous. Too many and it becomes too difficult to scan through your code, which is equally frustrating. </p>
<blockquote><p>Moderation is key. </p>
</blockquote>
<p></p>
<h2><span>7.</span> Learn Docblock and Use It</h2>
<p> If I could be sure every developer in the world would do one thing with absolute consistency, I think it would be the use of the <a href="http://pear.php.net/manual/en/standards.sample.php">Docblock commenting standard</a>. </p>
<p> I have a few reasons for my strong support of Docblock: </p>
<ol>
<li> It requires one to think about the <em>what</em> and <em>why</em> for each file, function, method, and so on. </li>
<li> It gives a clear description of the expected types for parameters and return values in functions/methods </li>
<li> It provides a quick description of what the code does </li>
<li> When coupled with one of the many IDEs that support Docblock, it creates code hinting (which allows you to see a description and expected parameters/return values for the method or function you&#8217;re using) </li>
</ol>
<p> This tip does border on the upper level of beginner, but I group this under &#8220;best practices to be learned as quickly as possible.&#8221; Feel free to skip it, but before you do, <strong>think about the kittens.</strong> </p>
<p> Docblock shows its versatility best when used to document a class: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code17'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17317"><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
</pre></td><td class="code" id="p173code17"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * A simple class to get the sum or difference of $_foo and a value
 *
 * @author Pat Herlihy
 * @copyright 2011 Pat Herlihy
 * @license http://www.opensource.org/licenses/mit-license.html
*/</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Pat_Test
<span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #009933; font-style: italic;">/**
  * The value to use in addition and subtraction
  * @var int
 */</span>
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_foo</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #009933; font-style: italic;">/**
  * Adds a value to $_foo and returns the sum *
  * @param int $add_me The value to add to $_foo
  * @return int The sum of $_foo and $add_me
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> add_to_foo<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$add_me</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_foo <span style="color: #339933;">+=</span> <span style="color: #000088;">$add_me</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #009933; font-style: italic;">/**
  * Subtracts a value from $_foo and returns the difference
  * @param int $subtract_me The value to subtract from $_foo
  * @return int The difference of $_foo and $subtract_me
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> subtract_from_foo<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$subtract_me</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_foo <span style="color: #339933;">-=</span> <span style="color: #000088;">$subtract_me</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p> At first it might look overwhelming, but <strong>the benefits are very much worth taking the time to familiarize yourself with the syntax.</strong> </p>
<p></p>
<h2><span>8.</span> Don&#8217;t Be Too Hardcore to Use an IDE</h2>
<p> If you don&#8217;t already know the type, you will soon: the developers who think <a href="http://en.wikipedia.org/wiki/Real_Programmer" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','en.wikipedia.org']);">real programmers don&#8217;t use IDEs.</a> </p>
<p> Now, look: if you want to impress people, learn to juggle. Refusing to use anything but <a href="http://www.gnu.org/software/emacs/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','www.gnu.org']);">Emacs</a> in the command line to write scripts will not get you chicks or grant you instant hacker status; it <em>will</em>, however, hang a  sign on your forehead warning your coworkers that you are, in fact, <strong>That Guy.</strong> </p>
<p> <strong><em>Don&#8217;t be That Guy.</em></strong> </p>
<blockquote>
<p> There is nothing wrong with using software to give you on-the-fly syntax highlighting, error-checking, and code hints. </p>
</blockquote>
<p> How in-depth your IDE goes is entirely up to you. Personally, I really like <a href="http://netbeans.org/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','netbeans.org']);">Netbeans</a>. I&#8217;ve heard tons of praise for <a href="http://panic.com/coda/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','panic.com']);">Coda</a> for Mac (though it&#8217;s not really an IDE), and I previously used <a href="http://eclipse.org/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','eclipse.org']);">Eclipse</a> before moving to Netbeans.  </p>
<p> Whatever IDE you choose, <strong>you&#8217;ll see your coding speed increase and your facepalm-worthy bugs decrease.</strong> Further, as you expand your code library, you&#8217;ll have code hinting for all of your custom software. (Because you&#8217;re using Docblock, right? Right?!) </p>
<p> Don&#8217;t think IDEs are uncool &#8212; no matter what &#8220;That Guy&#8221; tries to tell you. </p>
<p></p>
<h2><span>9.</span> Group Common Code Into Functions</h2>
<blockquote class="pullquote">
<p>If you see an action repeated, it&#8217;s time to strongly consider moving that code into a function.</p>
</blockquote>
<p> When you first start programming, it&#8217;s easy to start at the top of the page and work down, adding each piece of code right where it&#8217;s needed. </p>
<p> However, when you code this way, you&#8217;ll begin to notice that certain pieces of code are appearing over and over again. This is a minefield when it comes to maintenance and upgrades, because you have to hunt through each file for every occurrence of that action to change its functionality. </p>
<p> If you see an action repeated, even if it&#8217;s only twice, it&#8217;s time to strongly consider moving that code into a function. </p>
<p> Consider the following for example: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code18'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17318"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p173code18"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$unclean1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;a href=&quot;javascript:alert(\'Holy Crap!\');&quot;&gt;Click Me!&lt;/a&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$detagged1</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strip_tags"><span style="color: #990000;">strip_tags</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unclean1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$deslashed1</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/stripslashes"><span style="color: #990000;">stripslashes</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$detagged1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$clean1</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$deslashed1</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$unclean2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Let's call Björn!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$detagged2</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strip_tags"><span style="color: #990000;">strip_tags</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unclean2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$deslashed2</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/stripslashes"><span style="color: #990000;">stripslashes</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$detagged2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$clean2</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$deslashed2</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$clean1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clean2</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p> As you can see, both of those strings required a few steps before they could be considered safe to use. However, you&#8217;ll also notice that those same steps could be considered necessary for <em>every bit of information that is passed to the script.</em> </p>
<p> This is an instance where using a function instead is far more desirable: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code19'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17319"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p173code19"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$unclean1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;a href=&quot;javascript:alert(\'Holy Crap!\');&quot;&gt;Click Me!&lt;/a&gt;'</span><span style="color: #339933;">;</span> <span style="color: #000088;">$unclean2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Let's call Björn!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$clean1</span> <span style="color: #339933;">=</span> sanitize_input<span style="color: #009900;">&#40;</span><span style="color: #000088;">$unclean1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$clean2</span> <span style="color: #339933;">=</span> sanitize_input<span style="color: #009900;">&#40;</span><span style="color: #000088;">$unclean2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$clean1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clean2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> sanitize_input<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$input</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$detagged</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strip_tags"><span style="color: #990000;">strip_tags</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$deslashed</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/stripslashes"><span style="color: #990000;">stripslashes</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$detagged</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$deslashed</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p> By wrapping the common code in a function, it&#8217;s a bit easier to see what&#8217;s going on, and it&#8217;s much easier to edit the steps you want to take when sanitizing your input. </p>
<p></p>
<h2><span>10.</span> Group Related Functions Into Classes</h2>
<p> Getting a handle on OOP is another one of those things that I file under &#8220;best practices to learn as quickly as possible.&#8221; </p>
<blockquote>
<p> If you have a handful of functions that all deal with database actions, you can save yourself a lot of time and effort by grouping them into classes. </p>
</blockquote>
<p><a href="http://gigaspartan.com/2011/01/08/tutorial-object-oriented-php-for-beginners/">Learning object-oriented programming</a> is definitely outside the scope of this list, but I felt it was definitely worth mentioning in this beginners&#8217; list. </p>
<p></p>
<h2><span>11.</span> Use Constants, Not Globals</h2>
<blockquote class="pullquote">
<p>PHP allows you to define your own constants with the function <a href="http://php.net/define" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>define().</code></a></p>
</blockquote>
<p> When I first started programming on larger projects, I found myself using <a href="http://php.net/global" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);">global variables</a> more often than seemed necessary or reasonable. Admitting you have a problem is the first step.</p>
<p> I was storing things like application-wide data (such as the site&#8217;s name or the maximum image width) and database credentials in variables, and I found myself required to use the <a href="http://php.net/manual/en/reserved.variables.globals.php" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>$GLOBALS</code></a> superglobal to access this information. </p>
<p> Then I realized <strong>PHP allows you to define your own constants</strong> with the function <a href="http://php.net/define" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>define()</code></a>. </p>
<p> A constant is a great way to store information like the aforementioned app-wide data and database info. An additional bonus is that it can&#8217;t be modified, so you can&#8217;t accidentally overwrite your database password somewhere later in the script. </p>
<p> As a matter of best practices, the use of globals is generally discouraged to begin with, so the use of constants is preferred anyways. Review the following code for an example and see for yourself: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code20'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17320"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p173code20"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FOO'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'constant value'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'global value'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> baz<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> baz<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$constant</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' Constant: '</span> <span style="color: #339933;">.</span> FOO<span style="color: #339933;">;</span>
   <span style="color: #000088;">$global</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Global: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$constant</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$global</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h2><span>12.</span> Don&#8217;t Be Afraid to Use Includes</h2>
<p> Often, as you&#8217;re building larger products, it will make a lot of sense to break it apart into smaller chunks, or include files. </p>
<p> A generally accepted way to look at includes is to <strong>put any bit of code that will be used in multiple scripts into an include file</strong> (such as your database connection details, the header and footer data that is common across the whole site, utility functions like your input sanitization actions, etc.) so it can be pulled in by the file that needs it, rather than being copy-pasted. </p>
<p> For example, on a site with multiple pages, a standard template may emerge that looks something like this: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code21'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17321"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p173code21"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Application-wide data and database connection</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'constants.inc.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'database.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Utility functions</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'utilities.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Header markup</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'header.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* * Page-specific processing goes here */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Footer markup</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'footer.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h2><span>13.</span> Don&#8217;t Obsess Over Performance</h2>
<p> This is a point of near-paralysis for some developers, and it&#8217;s really too bad; there is a blurry line between writing efficient code and wasting time trying to squeeze an extra 5ms out of a script&#8217;s execution. </p>
<p> Definitely read a few performance articles and learn some of the major pitfalls that can drag your scripts to a slow crawl, but don&#8217;t waste extra time refactoring your code to change double quotes to single quotes because you found out it was a tiny fraction faster. </p>
<blockquote>
<p> Use your head, avoid the big problems, and keep your ears open, in case a tip you&#8217;ve never heard comes along to speed up your code, but don&#8217;t make it a race. </p>
</blockquote>
<p>No one can tell the difference between a 25ms page load and a 40ms page load. Make sure it&#8217;s not 700ms and move on to more important things. </p>
<p></p>
<h2><span>14.</span> Avoid Marrying HTML to Your Scripts</h2>
<p> This can be tricky, but do your best to avoid tangling up your HTML markup in your PHP. It&#8217;s nearly impossible to get away from it completely, but try to <strong>make sure that you don&#8217;t include any non-essential HTML markup in your code.</strong> </p>
<p> Consider the following: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code22'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17322"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p173code22"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;example-div&quot;&gt;&lt;p&gt;This is some test content.&lt;/p&gt;&lt;/div&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p> Was it necessary for that code to wrap the paragraph tag in a div? Could it have been modified to only include the paragraph tag that holds the text? Have a look at an alternative solution: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code23'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17323"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p173code23"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;example-div&quot;</span><span style="color: #339933;">&gt;</span>
   <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;p&gt;This is some test content.&lt;/p&gt;'</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p> <em>Note:</em> This example is grossly oversimplified. Generating HTML with PHP is usually more of an issue when dealing with a complex function or method that organizes a dataset. The point I&#8217;m trying to make is that it can sometimes be tempting to include more markup than is necessary in the output. </p>
<p> In most cases you can keep the HTML outside the PHP, which makes things easier to read and, usually, easier to work with as well. </p>
<p></p>
<h2><span>15.</span> Try to Use at Least One Unfamiliar Concept in Every Project</h2>
<blockquote class="pullquote">
<p>Push yourself outside your comfort zone.</p>
</blockquote>
<p> You&#8217;re never going to learn if you keep doing the same old thing. Try out a concept that you&#8217;re not quite comfortable with on every project you possibly can. </p>
<p>Don&#8217;t be over-ambitious, but definitely push yourself outside your comfort zone. It gives you a challenge, saves you from getting bored doing the same old thing over and over again, and forces you to progress as a developer. </p>
<p> Look at the project, find all the bits that you know well (or at least well enough), and then pick an area you&#8217;d like to understand. Sign up for it. Then do it. </p>
<p></p>
<h2><span>16.</span> Don&#8217;t Be Too Proud to Change</h2>
<p> You will be wrong. Frequently. But that&#8217;s not a bad thing. </p>
<p> As you improve, you&#8217;ll find newer, better solutions to problems that you&#8217;ve faced in the past. Don&#8217;t feel stupid;  you&#8217;re learning. </p>
<p> But it&#8217;s extremely important that you don&#8217;t become attached to the code you write. Don&#8217;t think your way is better just because it&#8217;s your way. If you happen across a great solution that makes yours look like a Band-Aid on a bullet wound, use it! Pay attention to what&#8217;s different and what you did that could have been better. File that away under, &#8220;Things I&#8217;ve Learned.&#8221; </p>
<blockquote>
<p> Never allow yourself to believe that an inelegant solution is acceptable because it&#8217;s yours. That&#8217;s hubris (which, if you&#8217;re not aware, doesn&#8217;t generally result in happy fun times). </p>
</blockquote>
<p></p>
<h2><span>17.</span> Validate</h2>
<p> If you&#8217;re a web programmer, start becoming familiar with <strong>input validation</strong> as soon as possible. </p>
<p> Keep in mind: <em>validation</em> is quite different from <em>sanitization</em>. </p>
<blockquote>
<p> Input validation is the practice of making sure data matches the format you&#8217;ve requested, like checking an email field for a valid email address or ensuring that a submitted username is 8-20 alphanumeric characters. </p>
</blockquote>
<p> It can be tedious and a pain in the ass, but making sure that only valid data makes it through your processing scripts will enhance the user experience and avoid a lot of bugs in the scripts that have to use the data later on.</p>
<p></p>
<h2><span>18.</span> Whitelists Instead of Blacklists</h2>
<blockquote class="pullquote">
<p> If you&#8217;re not on top of your blacklist, vulnerabilities appear. </p>
</blockquote>
<p> In plenty of situations, you&#8217;ll want to block or get rid of certain tags, words, email addresses, or other various bits of data. </p>
<p> A common solution is to use a <code>blacklist</code>: a collection of the tags, terms, etc. that aren&#8217;t allowed. </p>
<p> This poses a problem, however; you have to be more clever than the person trying to do something naughty. For instance, in the case of disabling JavaScript in posts, you might blacklist the <code>onclick</code> attribute, as well as most of the event attributes, but what if you forget one? What if a new one is added to the spec sometime in the future? </p>
<p> If you&#8217;re not on top of your blacklist, vulnerabilities appear. </p>
<p> However, to save headache later, use a <code>whitelist</code> whenever possible. A whitelist is the opposite of a blacklist: a collection of allowed tags, terms, etc. </p>
<p> For instance, in the <a href="http://php.net/strip-tags" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>strip_tags()</code></a> function, you can provide a whitelist to specify which tags are allowed in strings: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code24'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17324"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p173code24"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/strip_tags"><span style="color: #990000;">strip_tags</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;em&gt;&lt;strong&gt;&lt;tt&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p> Now your problem is most likely going to be that you can actually do <em>less</em> than you wanted, but that&#8217;s far safer and usually less of an emergency to handle. </p>
<p> You can&#8217;t get away with it in every situation, but saying what <em>is</em> allowed vs. what <em>isn&#8217;t</em> will provide you with more confidence and control over your scripts. </p>
<p></p>
<h2><span>19.</span> Learn to Count Like a Computer</h2>
<p> Are you looking for tip #20? Remember that in nearly all cases, <strong>counts in PHP start at 0,</strong> so this is actually the 20th tip.  You&#8217;ll find this to be the case in most languages; don&#8217;t let this one trip you up!</p>
<p></p>
<h2>Summary</h2>
<p> If you&#8217;re a beginner, the tips covered above will help you take great strides toward good habits and best practices. Don&#8217;t get overwhelmed if all of this is news to you; take things one step at a time (see tip #15). </p>
<p> Do you have a PHP tip for beginners? An addendum to one of the tips above? A question? <strong>Let us know in the comments!</strong> </p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2011/02/01/save-the-kittens-php-must-follows/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial: Object-Oriented PHP for Beginners</title>
		<link>http://gigaspartan.com/2011/01/08/tutorial-object-oriented-php-for-beginners/</link>
		<comments>http://gigaspartan.com/2011/01/08/tutorial-object-oriented-php-for-beginners/#comments</comments>
		<pubDate>Sun, 09 Jan 2011 03:28:09 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=157</guid>
		<description><![CDATA[For many PHP programmers, object-oriented programming is a frightening concept, full of complicated syntax and other roadblocks. In this tutotial you’ll learn the concepts behind object-oriented programming (OOP), a style of coding in which related actions are grouped into classes to aid in creating more-compact, effective code. Understanding Object-Oriented Programming Object-oriented programming is a style [...]]]></description>
			<content:encoded><![CDATA[<p>
For many PHP programmers, object-oriented programming is a frightening concept, full of complicated syntax and other roadblocks. In this tutotial you’ll learn the concepts behind <strong>object-oriented programming</strong> (OOP), a style of coding in which related actions are grouped into classes to aid in creating more-compact, effective code.
</p>
<p></p>
<h2>Understanding Object-Oriented Programming</h2>
<p>
Object-oriented programming is a style of coding that allows developers to group similar tasks into <strong>classes</strong>. This helps keep code following the tenet <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">“don’t repeat yourself” (DRY)</a> and easy-to-maintain.
</p>
<blockquote class="pullquote pqRight">
<p>
“Object-oriented programming is a style of coding that allows developers to group similar tasks into <strong>classes</strong>.”
</p>
</blockquote>
<p><span id="more-157"></span></p>
<p>
One of the major benefits of DRY programming is that, if a piece of information changes in your program, usually <strong>only one change is required to update the code.</strong> One of the biggest nightmares for developers is maintaining code where data is declared over and over again, meaning any changes to the program become an infinitely more frustrating game of <em>Where’s Waldo?</em> as they hunt for duplicated data and functionality.
</p>
<p>
OOP is intimidating to a lot of developers because it introduces new syntax and, at a glance, appears to be far more complex than simple procedural, or inline, code. However, upon closer inspection, OOP is actually a very straightforward and ultimately simpler approach to programming.
</p>
<p></p>
<h2>Understanding Objects and Classes</h2>
<p>
Before you can get too deep into the finer points of OOP, a basic understanding of the differences between <strong>objects</strong> and <strong>classes</strong> is necessary. This section will go over the building blocks of classes, their different capabilities, and some of their uses.
</p>
<h3>Recognizing the Differences Between Objects and Classes</h3>
<div class="tutorial_image">
    <img src="http://d2o0t5hpnwv4c1.cloudfront.net/734_oop/images/blueprint-houses.jpg" border="0">
</p>
<p>
        Photos by <a href="http://www.flickr.com/photos/instantjefferson/">Instant Jefferson</a> and <a href="http://www.flickr.com/photos/johnwardell/">John Wardell</a>
    </p>
</div>
<blockquote class="pullquote pqRight">
<p>
“Developers start talking about objects and classes, and they appear to be interchangeable terms. This is not the case, however.”
</p>
</blockquote>
<p>
Right off the bat, there’s confusion in OOP: seasoned developers start talking about objects and classes, and they appear to be interchangeable terms. This is not the case, however, though the difference can be tough to wrap your head around at first.
</p>
<p>
A class, for example, is like <strong>a blueprint for a house</strong>. It defines the shape of the house on paper, with relationships between the different parts of the house clearly defined and planned out, even though the house doesn’t exist.
</p>
<p>
An object, then, is like <strong>the actual house</strong> built according to that blueprint. The data stored in the object is like the wood, wires, and concrete that compose the house: without being assembled according to the blueprint, it’s just a pile of stuff. However, when it all comes together, it becomes an organized, useful house.
</p>
<p>
<strong>Classes form the structure of data and actions and use that information to build objects.</strong> More than one object can be built from the same class at the same time, each one independent of the others. Continuing with our construction analogy, it’s similar to the way an entire subdivision can be built from the same blueprint: 150 different houses that all look the same but have different<br />
families and decorations inside.
</p>
<h3>Structuring Classes</h3>
<p>
The syntax to create a class is pretty straightforward: declare a class using the <tt>class</tt> keyword, followed by the name of the class and a set of curly braces (<tt>{}</tt>):
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code70'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15770"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p157code70"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Class properties and methods go here</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
After creating the class, a new class can be instantiated and stored in a variable using the <tt>new</tt> keyword:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code71'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15771"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p157code71"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
To see the contents of the class, use <tt>var_dump()</tt>:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code72'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15772"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p157code72"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
Try out this process by putting all the preceding code in a new file called <tt>test.php</tt> in [your local] testing folder:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code73'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15773"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p157code73"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Class properties and methods go here</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Load the page in your browser at <tt>http://localhost/test.php</tt> and the following should display:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code74'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15774"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p157code74"><pre class="php" style="font-family:monospace;">object<span style="color: #009900;">&#40;</span>MyClass<span style="color: #009900;">&#41;</span><span style="color: #666666; font-style: italic;">#1 (0) { }</span></pre></td></tr></table></div>

<p>
<strong>In its simplest form, you’ve just completed your first OOP script.</strong>
</p>
<p></p>
<h2>Defining Class Properties</h2>
<p>
To add data to a class, <strong>properties</strong>, or class-specific variables, are used. These work exactly like regular variables, except they’re bound to the object and therefore can only be accessed using the object.
</p>
<p>
To add a property to <tt>MyClass</tt>, add the following code to your script:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code75'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15775"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p157code75"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
The keyword <tt>public</tt> determines the visibility of the property, which you’ll learn about a little later in this chapter. Next, the property is named using standard variable syntax, and a value is assigned (though class properties do not need an initial value).
</p>
<p>
To read this property and output it to the browser, reference the object from which to read and the property to be read:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code76'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15776"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p157code76"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
Because multiple instances of a class can exist, if the individual object is not referenced, the script would be unable to determine which object to read from. The use of the arrow (<tt>-></tt>) is an OOP construct that accesses the contained properties and methods of a given object.
</p>
<p>
Modify the script in <tt>test.php</tt> to read out the property rather than dumping the whole class by modifying the code as shown:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code77'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15777"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p157code77"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Output the property</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Reloading your browser now outputs the following:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code78'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15778"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p157code78"><pre class="php" style="font-family:monospace;">I<span style="color: #0000ff;">'m a class property!</span></pre></td></tr></table></div>

<p></p>
<h2>Defining Class Methods</h2>
<p>
<strong>Methods</strong> are class-specific functions. Individual actions that an object will be able to perform are defined within the class as methods.
</p>
<p>
For instance, to create methods that would set and get the value of the class property <tt>$prop1</tt>, add the following to your code:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code79'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15779"><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
</pre></td><td class="code" id="p157code79"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
<strong>Note</strong> — OOP allows objects to reference themselves using <tt>$this</tt>. When working within a method, use <tt>$this</tt> in the same way you would use the object name outside the class.
</p>
<p>
To use these methods, call them just like regular functions, but first, reference the object they belong to. Read the property from <tt>MyClass</tt>, change its value, and read it out again by making the modifications below:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code80'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15780"><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
</pre></td><td class="code" id="p157code80"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Get the property value</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I'm a new property value!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Set a new one</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Read it out again to show the change</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Reload your browser, and you’ll see the following:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code81'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15781"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p157code81"><pre class="php" style="font-family:monospace;">I<span style="color: #0000ff;">'m a class property!
I'</span>m a <span style="color: #000000; font-weight: bold;">new</span> property value<span style="color: #339933;">!</span></pre></td></tr></table></div>

<blockquote>
<p>
“The power of OOP becomes apparent when using multiple instances of the<br />
same class.”
</p>
</blockquote>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code82'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15782"><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
</pre></td><td class="code" id="p157code82"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</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;">// Create two objects</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
<span style="color: #000088;">$obj2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1 from both objects</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</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;">// Set new values for both objects</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I'm a new property value!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$obj2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I belong to the second instance!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output both objects' $prop1 value</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
When you load the results in your browser, they read as follows:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code83'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15783"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code83"><pre class="php" style="font-family:monospace;">I<span style="color: #0000ff;">'m a class property!
I'</span>m a <span style="color: #000000; font-weight: bold;">class</span> property<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a new property value!
I belong to the second instance!</span></pre></td></tr></table></div>

<p>
As you can see, <strong>OOP keeps objects as separate entities</strong>, which makes for easy separation of different pieces of code into small, related bundles.
</p>
<p></p>
<h2>Magic Methods in OOP</h2>
<p>
To make the use of objects easier, PHP also provides a number of <strong>magic methods</strong>, or special methods that are called when certain common actions occur within objects. This allows developers to perform a number of useful tasks with relative ease.
</p>
<h3>Using Constructors and Destructors</h3>
<p>
When an object is instantiated, it’s often desirable to set a few things right off the bat. To handle this, PHP provides the magic method <tt>__construct()</tt>, which is called automatically whenever a new object is<br />
created.
</p>
<p>
For the purpose of illustrating the concept of constructors, add a constructor to <tt>MyClass</tt> that will output a message whenever a new instance of the class is created:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code84'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15784"><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
</pre></td><td class="code" id="p157code84"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</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;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</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;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
<strong>Note</strong> — <tt>__CLASS__</tt> returns the name of the class in which it is called; this is what is known as a <a href="http://us3.php.net/manual/en/language.constants.predefined.php">magic constant</a>. There are several available magic constants, which you can read more about in the PHP manual.
</p>
<p>
Reloading the file in your browser will produce the following result:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code85'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15785"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p157code85"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a class property!
End of file.</span></pre></td></tr></table></div>

<p>
To call a function when the object is destroyed, the <tt>__destruct()</tt> magic method is available. This is useful for class cleanup (closing a database connection, for instance).
</p>
<p>
Output a message when the object is destroyed by defining the magic method<br />
<tt>__destruct()</tt> in <tt>MyClass</tt>:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code86'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15786"><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
</pre></td><td class="code" id="p157code86"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</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;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</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;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
With a destructor defined, reloading the test file results in the following output:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code87'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15787"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code87"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a class property!
End of file.
The class &quot;MyClass&quot; was destroyed.</span></pre></td></tr></table></div>

<blockquote>
<p>
“When the end of a file is reached, PHP automatically releases all resources.”
</p>
</blockquote>
<p>
To explicitly trigger the destructor, you can destroy the object using the<br />
function <tt>unset()</tt>:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code88'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15788"><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
</pre></td><td class="code" id="p157code88"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</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;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</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;">// Destroy the object</span>
<a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Now the result changes to the following when loaded in your browser:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code89'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15789"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code89"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.
End of file.</span></pre></td></tr></table></div>

<h3>Converting to a String</h3>
<p>
To avoid an error if a script attempts to output <tt>MyClass</tt> as a string, another magic method is used called <tt>__toString()</tt>.
</p>
<p>
Without <tt>__toString()</tt>, <em>attempting to output the object as a string results in a fatal error</em>. Attempt to use <tt>echo</tt> to output the object without a magic method in place:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code90'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15790"><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
</pre></td><td class="code" id="p157code90"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</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;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy the object</span>
<a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
This results in the following:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code91'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15791"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p157code91"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
&nbsp;
Catchable fatal error<span style="color: #339933;">:</span> Object of <span style="color: #000000; font-weight: bold;">class</span> MyClass could not be converted to string in <span style="color: #339933;">/</span>Applications<span style="color: #339933;">/</span>XAMPP<span style="color: #339933;">/</span>xamppfiles<span style="color: #339933;">/</span>htdocs<span style="color: #339933;">/</span>testing<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">40</span></pre></td></tr></table></div>

<p>
To avoid this error, add a <tt>__toString()</tt> method:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code92'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15792"><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
</pre></td><td class="code" id="p157code92"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> __toString<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;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</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;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy the object</span>
<a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
In this case, attempting to convert the object to a string results in a call to the <tt>getProperty()</tt> method. Load the test script in your browser to see the result:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code93'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15793"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code93"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
Using the toString method<span style="color: #339933;">:</span> I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.
End of file.</span></pre></td></tr></table></div>

<p>
<strong>Tip</strong> — In addition to the magic methods discussed in this section, several others are available. For a complete list of magic methods, see the  <a href="http://us2.php.net/manual/en/language.oop5.magic.php">PHP manual page</a>.
</p>
<p></p>
<h2>Using Class Inheritance</h2>
<p>
<strong>Classes can inherit the methods and properties of another class</strong> using the <tt>extends</tt> keyword. For instance, to create a second class that extends <tt>MyClass</tt> and adds a method, you would add the following to your test file:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code94'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15794"><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
</pre></td><td class="code" id="p157code94"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> __toString<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;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<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;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">newMethod</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;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Upon reloading the test file in your browser, the following is output:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code95'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15795"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code95"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
From a <span style="color: #000000; font-weight: bold;">new</span> method in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></td></tr></table></div>

<h3>Overwriting Inherited Properties and Methods</h3>
<p>
To change the behavior of an existing property or method in the new class, you can simply overwrite it by declaring it again in the new class:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code96'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15796"><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
</pre></td><td class="code" id="p157code96"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> __toString<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;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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> newMethod<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;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">newMethod</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;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
This changes the output in the browser to:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code97'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15797"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code97"><pre class="php" style="font-family:monospace;">A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
From a <span style="color: #000000; font-weight: bold;">new</span> method in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></td></tr></table></div>

<h3>Preserving Original Method Functionality While Overwriting Methods</h3>
<p>
To add new functionality to an inherited method while keeping the original method intact, use the <tt>parent</tt> keyword with the <strong>scope resolution operator</strong> (<tt>::</tt>):
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code98'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15798"><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
</pre></td><td class="code" id="p157code98"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> __toString<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;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</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> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Call the parent class's constructor</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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> newMethod<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;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">newMethod</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;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
This outputs the result of both the parent constructor and the new class’s constructor:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code99'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15799"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p157code99"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
From a <span style="color: #000000; font-weight: bold;">new</span> method in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></td></tr></table></div>

<p></p>
<h2>Assigning the Visibility of Properties and Methods</h2>
<p>
For added control over objects, methods and properties are assigned visibility. This controls how and from where properties and methods can be accessed. There are three visibility keywords: <tt>public</tt>, <tt>protected</tt>, and <tt>private</tt>. In addition to its visibility, a method or property can be declared as <tt>static</tt>, which allows them to be accessed without an instantiation of the class.
</p>
<blockquote class="pullquote pqRight"><p>“For added control over objects, methods and properties are assigned visibility.”</p>
</blockquote>
<p>
<strong>Note</strong> — Visibility is a new feature as of PHP 5. For information on <a href="http://us2.php.net/manual/en/language.oop5.php">OOP compatibility with PHP 4</a>, see the PHP manual page.
</p>
<h3>Public Properties and Methods</h3>
<p>
All the methods and properties you’ve used so far have been public. This means that they can be accessed anywhere, both within the class and externally.
</p>
<h3>Protected Properties and Methods</h3>
<p>
When a property or method is declared <tt>protected</tt>, <strong>it can only be accessed within the class itself or in descendant classes</strong> (classes that extend the class containing the protected method).
</p>
<p>
Declare the <tt>getProperty()</tt> method as protected in <tt>MyClass</tt> and try to access it directly from outside the class:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code100'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157100"><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
</pre></td><td class="code" id="p157code100"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> __toString<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;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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> newMethod<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;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Attempt to call a protected method</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Upon attempting to run this script, the following error shows up:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code101'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157101"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code101"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
&nbsp;
Fatal error<span style="color: #339933;">:</span> Call to <span style="color: #000000; font-weight: bold;">protected</span> method MyClass<span style="color: #339933;">::</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> from context <span style="color: #0000ff;">''</span> in <span style="color: #339933;">/</span>Applications<span style="color: #339933;">/</span>XAMPP<span style="color: #339933;">/</span>xamppfiles<span style="color: #339933;">/</span>htdocs<span style="color: #339933;">/</span>testing<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">55</span></pre></td></tr></table></div>

<p>
Now, create a new method in <tt>MyOtherClass</tt> to call the <tt>getProperty()</tt> method:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code102'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157102"><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
</pre></td><td class="code" id="p157code102"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> __toString<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;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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> newMethod<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;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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> callProtected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Call the protected method from within a public method</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callProtected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
This generates the desired result:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code103'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157103"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code103"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></td></tr></table></div>

<h3>Private Properties and Methods</h3>
<p>
A property or method declared <tt>private</tt> is accessible <strong>only from within the class that defines it</strong>. This means that <em>even if a new class extends the class that defines a private property,</em> that property or method will not be available at all within the child class.
</p>
<p>
To demonstrate this, declare <tt>getProperty()</tt> as private in <tt>MyClass</tt>, and attempt to call <tt>callProtected()</tt> from<br />
<tt>MyOtherClass</tt>:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code104'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157104"><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
</pre></td><td class="code" id="p157code104"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> __toString<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;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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> newMethod<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;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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> callProtected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callProtected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Reload your browser, and the following error appears:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code105'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157105"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code105"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
&nbsp;
Fatal error<span style="color: #339933;">:</span> Call to <span style="color: #000000; font-weight: bold;">private</span> method MyClass<span style="color: #339933;">::</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> from context <span style="color: #0000ff;">'MyOtherClass'</span> in <span style="color: #339933;">/</span>Applications<span style="color: #339933;">/</span>XAMPP<span style="color: #339933;">/</span>xamppfiles<span style="color: #339933;">/</span>htdocs<span style="color: #339933;">/</span>testing<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">49</span></pre></td></tr></table></div>

<h3>Static Properties and Methods</h3>
<p>
A method or property declared <tt>static</tt> can be accessed without first instantiating the class; you simply supply the class name, scope resolution operator, and the property or method name.
</p>
<blockquote>
<p>
“One of the major benefits to using static properties is that they keep their stored values for the duration of the script.”
</p>
</blockquote>
<p>
To demonstrate this, add a static property called <tt>$count</tt> and a static method called <tt>plusOne()</tt> to <tt>MyClass</tt>. Then set up a <tt>do...while</tt> loop to output the incremented value of <tt>$count</tt> as long as the value is less than 10:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code106'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157106"><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
</pre></td><td class="code" id="p157code106"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</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> __destruct<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;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</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> __toString<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;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> plusOne<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;The count is &quot;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">++</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$count</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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> newMethod<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;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</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> callProtected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">do</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Call plusOne without instantiating MyClass</span>
    <span style="color: #b1b100;">echo</span> MyClass<span style="color: #339933;">::</span><span style="color: #004000;">plusOne</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> MyClass<span style="color: #339933;">::</span><span style="color: #000088;">$count</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
<strong>Note</strong> — When accessing static properties, the dollar sign<br />
(<tt>$</tt>) comes <em>after the scope resolution operator.</em>
</p>
<p>
When you load this script in your browser, the following is output:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code107'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157107"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p157code107"><pre class="php" style="font-family:monospace;">The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">1</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">2</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">3</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">4</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">5</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">6</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">7</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">8</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">9</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">10</span><span style="color: #339933;">.</span></pre></td></tr></table></div>

<p></p>
<h2>Commenting with DocBlocks</h2>
<blockquote class="pullquote pqRight">
<p>
“The DocBlock commenting style is a widely<br />
accepted method of documenting classes.”
</p>
</blockquote>
<p>
While not an official part of OOP, the <a href="http://en.wikipedia.org/wiki/PHPDoc">DocBlock</a> commenting style is a widely accepted method of documenting classes. Aside from providing a standard for<br />
developers to use when writing code, it has also been adopted by many of the most popular software development kits (SDKs), such as <a href="http://eclipse.org">Eclipse</a> and <a href="http://netbeans.org">NetBeans</a>, and will be used to generate code hints.
</p>
<p>
A DocBlock is defined by using a block comment that starts with an additional asterisk:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code108'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157108"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p157code108"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * This is a very basic DocBlock
 */</span></pre></td></tr></table></div>

<p>
The real power of DocBlocks comes with the ability to use <strong>tags</strong>, which start with an at symbol (<tt>@</tt>) immediately followed by the tag name and the value of the tag. <strong>DocBlock tags allow developers to define authors of a file, the license for a class, the property or method information, and other useful information.</strong>
</p>
<p>
The most common tags used follow:
</p>
<ul>
<li><strong>@author</strong>: The author of the current element (which might be a class, file, method, or any bit of code) are listed using this tag. Multiple author tags can be used in the same DocBlock if more than one author is credited. The format for the author name is <tt>John Doe <john.doe@email.com></tt>.</li>
<li><strong>@copyright</strong>: This signifies the copyright year and name of the copyright holder for the current element. The format is <tt>2010 Copyright Holder</tt>.</li>
<li><strong>@license</strong>: This links to the license for the current element. The format for the license information is<br />
        <tt>http://www.example.com/path/to/license.txt License Name</tt>.</li>
<li><strong>@var</strong>: This holds the type and description of a variable or class property. The format is <tt>type element description</tt>.</li>
<li><strong>@param</strong>: This tag shows the type and description of a function or method parameter. The format is <tt>type $element_name element description</tt>.</li>
<li><strong>@return</strong>: The type and description of the return value of a function or method are provided in this tag. The format is <tt>type return element description</tt>.</li>
</ul>
<p>
A sample class commented with DocBlocks might look like this:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code109'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157109"><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
</pre></td><td class="code" id="p157code109"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A simple class
 *
 * This is the long description for this class,
 * which can span as many lines as needed. It is
 * not required, whereas the short description is
 * necessary.
 *
 * It can also span multiple paragraphs if the
 * description merits that much verbiage.
 *
 * @author Jason Lengstorf &lt;jason.lengstorf@ennuidesign.com&gt;
 * @copyright 2010 Ennui Design
 * @license http://www.php.net/license/3_01.txt PHP License 3.01
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> SimpleClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * A public variable
     *
     * @var string stores data for the class
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$foo</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Sets $foo to a new value upon class instantiation
     *
     * @param string $val a value required for the class
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Multiplies two integers
     *
     * Accepts a pair of integers and returns the
     * product of the two.
     *
     * @param int $bat a number to be multiplied
     * @param int $baz a number to be multiplied
     * @return int the product of the two parameters
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$bat</span><span style="color: #339933;">,</span> <span style="color: #000088;">$baz</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$bat</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$baz</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Once you scan the preceding class, the benefits of DocBlock are apparent: everything is clearly defined so that the next developer can pick up the code and <em>never have to wonder what a snippet of code does or what it should contain.</em>
</p>
<p></p>
<h2>Comparing Object-Oriented and Procedural Code</h2>
<p>
There’s not really a right and wrong way to write code. That being said, <strong>this section outlines a strong argument for adopting an object-oriented approach in software development, especially in large applications.</strong>
</p>
<p></p>
<h2>Reason 1: Ease of Implementation</h2>
<blockquote class="pullquote pqRight">
<p>
“While it may be daunting at first, OOP actually provides an easier approach to dealing with data.”
</p>
</blockquote>
<p>
While it may be daunting at first, OOP actually provides an easier approach to dealing with data. Because an object can store data internally, variables don’t need to be passed from function to function to work properly.
</p>
<p>
Also, because <em>multiple instances of the same class can exist simultaneously</em>, dealing with large data sets is infinitely easier. For instance, imagine you have two people’s information being processed in a file. They need names, occupations, and ages.
</p>
<h3>The Procedural Approach</h3>
<p>
Here’s the procedural approach to our example:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code110'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157110"><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
</pre></td><td class="code" id="p157code110"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> changeJob<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newjob</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$person</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'job'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newjob</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Change the person's job</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$person</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">++</span><span style="color: #000088;">$person</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Add 1 to the person's age</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$person</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Tom'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'job'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Button-Pusher'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">34</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$person2</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'job'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Lever-Puller'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">41</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the starting values for the people</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 1: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</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: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 2: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</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: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Tom got a promotion and had a birthday</span>
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> changeJob<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Box-Mover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// John just had a birthday</span>
<span style="color: #000088;">$person2</span> <span style="color: #339933;">=</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the new values for the people</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 1: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</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: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 2: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</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: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
When executed, the code outputs the following:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code111'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157111"><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
</pre></td><td class="code" id="p157code111"><pre class="php" style="font-family:monospace;">Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Button<span style="color: #339933;">-</span>Pusher
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">34</span>
<span style="color: #009900;">&#41;</span>
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever<span style="color: #339933;">-</span>Puller
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">41</span>
<span style="color: #009900;">&#41;</span>
Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Box<span style="color: #339933;">-</span>Mover
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span>
<span style="color: #009900;">&#41;</span>
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever<span style="color: #339933;">-</span>Puller
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">42</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>
While this code isn’t necessarily bad, there’s a lot to keep in mind while coding. <strong>The array of the affected person’s attributes must be passed and returned from each function call</strong>, which leaves margin for error.
</p>
<p>
To clean up this example, it would be desirable to <strong>leave as few things up to the developer as possible.</strong> Only absolutely essential information for the current operation should need to be passed to the functions.
</p>
<p>
<strong>This is where OOP steps in and helps you clean things up.</strong>
</p>
<h3>The OOP Approach</h3>
<p>
Here’s the OOP approach to our example:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code112'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157112"><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
</pre></td><td class="code" id="p157code112"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Person
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_name</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_job</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_age</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$job</span><span style="color: #339933;">,</span> <span style="color: #000088;">$age</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_name <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_job <span style="color: #339933;">=</span> <span style="color: #000088;">$job</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_age <span style="color: #339933;">=</span> <span style="color: #000088;">$age</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> changeJob<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newjob</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_job <span style="color: #339933;">=</span> <span style="color: #000088;">$newjob</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> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">++</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_age<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;">// Create two new people</span>
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tom&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Button-Pusher&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">34</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;John&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Lever Puller&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">41</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output their starting point</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 1: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</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: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 2: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</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: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Give Tom a promotion and a birthday</span>
<span style="color: #000088;">$person1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">changeJob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Box-Mover&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">happyBirthday</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;">// John just gets a year older</span>
<span style="color: #000088;">$person2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">happyBirthday</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;">// Output the ending values</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 1: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</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: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 2: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</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: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
This outputs the following in the browser:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code113'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157113"><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
</pre></td><td class="code" id="p157code113"><pre class="php" style="font-family:monospace;">Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Button<span style="color: #339933;">-</span>Pusher
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">34</span>
<span style="color: #009900;">&#41;</span>
&nbsp;
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever Puller
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">41</span>
<span style="color: #009900;">&#41;</span>
&nbsp;
Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Box<span style="color: #339933;">-</span>Mover
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span>
<span style="color: #009900;">&#41;</span>
&nbsp;
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever Puller
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">42</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>
There’s a little bit more setup involved to make the approach object oriented, but after the class is defined, creating and modifying people is a breeze; <strong>a person’s information does not need to be passed or returned from methods, and only absolutely essential information is passed to each method.</strong>
</p>
<blockquote class="pullquote pqRight">
<p>
“OOP will significantly reduce your workload if implemented properly.”
</p>
</blockquote>
<p>
On the small scale, this difference may not seem like much, but as your applications grow in size, OOP will significantly reduce your workload if implemented properly.
</p>
<p>
<strong>Tip</strong> — <em>Not everything needs to be object oriented.</em> A quick function that handles something small in one place inside the application does not necessarily need to be wrapped in a class. Use your best judgment when deciding between object-oriented and procedural approaches.
</p>
<p></p>
<h2>Reason 2: Better Organization</h2>
<p>
Another benefit of OOP is how well it lends itself to being <strong>easily packaged and cataloged.</strong> Each class can generally be kept in its own separate file, and if a uniform naming convention is used, accessing the classes is extremely simple.
</p>
<p>
Assume you’ve got an application with 150 classes that are called dynamically through a controller file at the root of your application filesystem. All 150 classes follow the naming convention <tt>class.classname.inc.php</tt> and reside in the <tt>inc</tt> folder of your application.
</p>
<p>
The controller can implement PHP’s <tt>__autoload()</tt> function to dynamically pull in only the classes it needs as they are called, rather than including all 150 in the controller file just in case or coming up with some clever way of including the files in your own code:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code114'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157114"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p157code114"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_name</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">'inc/class.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$class_name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.inc.php'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Having each class in a separate file also makes code more portable and easier to reuse in new applications without a bunch of copying and pasting.
</p>
<p></p>
<h2>Reason 3: Easier Maintenance</h2>
<p>
Due to the more compact nature of OOP when done correctly, <strong>changes in the code are usually much easier to spot</strong> and make than in a long spaghetti code procedural implementation.
</p>
<p>
If a particular array of information gains a new attribute, a procedural piece of software may require (in a worst-case scenario) that the new attribute be added to each function that uses the array.
</p>
<p>
An OOP application could potentially be updated as easily adding the new property and then adding the methods that deal with said property.
</p>
<p>
A lot of the benefits covered in this section are the product of <strong>OOP in combination with DRY programming practices.</strong> It is definitely possible to create easy-to-maintain procedural code that doesn’t cause nightmares, and it is equally possible to create awful object-oriented code.
</p>
<p></p>
<h2>Summary</h2>
<p>
At this point, you should feel comfortable with the object-oriented programming style. Learning OOP is a great way to take your programming to that next level. When implemented properly, OOP will help you produce easy-to-read, easy-to-maintain, portable code that will save you (and the developers who work with you) hours of extra work. Are you stuck on something that wasn’t covered in this article? Are you already using OOP and have some tips for beginners? Share them in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2011/01/08/tutorial-object-oriented-php-for-beginners/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a PHP/MySQL Powered Forum from Scratch</title>
		<link>http://gigaspartan.com/2011/01/03/tutorial-create-a-phpmysql-powered-forum-from-scratch/</link>
		<comments>http://gigaspartan.com/2011/01/03/tutorial-create-a-phpmysql-powered-forum-from-scratch/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 06:57:35 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[login system]]></category>
		<category><![CDATA[membership]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[register]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[user system]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=141</guid>
		<description><![CDATA[In this tutorial, we’re going to build a PHP/MySQL powered forum from scratch. This tutorial is perfect for getting used to basic PHP and database usage. Let’s dive right in! Step 1: Creating Database Tables It’s always a good idea to start with creating a good data model when building an application. Let’s describe our [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, we’re going to build a PHP/MySQL powered forum from scratch. This tutorial is perfect for getting used to basic PHP and database usage. Let’s dive right in!</p>
<p><span id="more-10188"></span></p>
<h2><span>Step 1</span>: Creating Database Tables</h2>
<p>
It’s always a good idea to start with creating a good data model when building an application. Let’s describe our application in one sentence: We are going to make a forum which has <strong>users</strong> who create <strong>topics</strong> in various <strong>categories</strong>. Other users can <strong>post</strong> replies. As you can see, I highlighted a couple of nouns which represent our table names.
</p>
<h4>Users</h4>
<ul>
<li>Categories</li>
<li>Topics</li>
<li>Posts</li>
</ul>
<p>
	These three objects are related to each other, so we’ll process that in our table design. Take a look at the scheme below.
</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/606_forum/tut_files/1.png" border="0"></div>
<p>
	Looks pretty neat, huh? Every square is a database table. All the columns are listed in it and the lines between them represent the relationships. I’ll  explain them further, so it’s okay if it doesn’t make a lot of sense to you right now.</p>
<p><span id="more-141"></span></p>
<p>
	I’ll discuss each table by explaining the SQL, which I created using the scheme above. For your own scripts you can create a similar scheme and SQL too. Some editors like MySQL Workbench (the one I used) can generate .sql files too, but I would recommend learning SQL because it’s more fun to do it yourself. A SQL introduction can be found at <a href="http://w3schools.com/sql/sql_intro.asp">W3Schools</a>.
</p>
<h5>Users Table</h5>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code154'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141154"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p141code154"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> users <span style="color: #66cc66;">&#40;</span>
user_id 	INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
user_name	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
user_pass  	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
user_email	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
user_date	DATETIME <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
user_level	INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> user_name_unique <span style="color: #66cc66;">&#40;</span>user_name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>user_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> TYPE<span style="color: #66cc66;">=</span>INNODB;</pre></td></tr></table></div>

<p>
	The CREATE TABLE statement is used to indicate we want to create a new table, of course. The statement is followed by the name of the table and all the columns are listed between the brackets. The names of all the fields are self-explanatory, so we’ll only discuss the data types below.
	</p>
<h5>user_id</h5>
<blockquote class="pullquote pqRight">
<p>
“A primary key is used to uniquely identify each row in a table.”
</p>
</blockquote>
<p>
		The type of this field is INT, which means this field holds an integer. The field cannot be empty (NOT NULL) and increments which each record inserted. At the bottom of the table you can see the user_id field is declared as a primary key. A primary key is used to uniquely identify each row in a table. No two distinct rows in a table can have the same value (or combination of values) in all columns. That might be a bit unclear, so here’s a little example.
		</p>
<p>
		There is a user called John Doe. If another users registers with the same name, there’s a problem, because: which user is which? You can’t tell and the database can’t tell either. By using a primary key this problem is solved, because both topics are unique.
		</p>
<p>
		All the other tables have got primary keys too and they work the same way.
	</p>
<h5>user_name</h5>
<p>		This is a text field, called a VARCHAR field in MySQL. The number between brackets is the maximum length. A user can choose a username up to 30 characters long. This field cannot be NULL. At the bottom of the table you can see this field is declared UNIQUE, which means the same username cannot be registered twice. The UNIQUE INDEX part tells the database we want to add a unique key. Then we define the name of the unique key, user_name_unique in this case. Between brackets is the field the unique key applies to, which is user_name.
	</p>
<h5>user_pass</h5>
<p>
		This field is equal to the user_name field, except the maximum length. Since the user password, no matter what length, is hashed with sha1(), the password will always be 40 characters long.
	</p>
<h5>user_email</h5>
<p>
		This field is equal to the user_pass field.
		</p>
<h5>user_date</h5>
<p>
		This is a field in which we’ll store the date the user registered. It’s type is DATETIME and the field cannot be NULL.</p>
<h5>user_level</h5>
<p>
		This field contains the level of the user, for example: ’0′ for a regular user and ’1′ for an admin. More about this later.</p>
</p>
<h4>Categories Table</h4>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code155'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141155"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p141code155"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> categories <span style="color: #66cc66;">&#40;</span>
cat_id 		 	INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
cat_name	 	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
cat_description 	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> cat_name_unique <span style="color: #66cc66;">&#40;</span>cat_name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>cat_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> TYPE<span style="color: #66cc66;">=</span>INNODB;</pre></td></tr></table></div>

<p>
These data types basically work the same way as the ones in the users table. This table also has a primary key and the name of the category must be an unique one.
</p>
<h4>Topics Table</h4>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code156'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141156"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p141code156"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> topics <span style="color: #66cc66;">&#40;</span>
topic_id		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
topic_subject  		VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
topic_date		DATETIME <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
topic_cat		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
topic_by		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>topic_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> TYPE<span style="color: #66cc66;">=</span>INNODB;</pre></td></tr></table></div>

<p>
		This table is almost the same as the other tables, except for the topic_by field. That field refers to the user who created the topic. The topic_cat refers to the category the topic belongs to. We cannot force these relationships by just declaring the field. We have to let the database know this field must contain an existing user_id from the users table, or a valid cat_id from the categories table. We’ll add some relationships after I’ve discussed the posts table.
	</p>
<h4>Posts Table</h4>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code157'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141157"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p141code157"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> posts <span style="color: #66cc66;">&#40;</span>
post_id 		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
post_content		TEXT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
post_date 		DATETIME <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
post_topic		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
post_by		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>post_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> TYPE<span style="color: #66cc66;">=</span>INNODB;</pre></td></tr></table></div>

<p>
	This is the same as the rest of the tables; there’s also a field which refers to a user_id here: the post_by field. The post_topic field refers to the topic the post belongs to.
	</p>
<blockquote class="pullquote pqRight">
<p>
“A foreign key is a referential constraint between two tables. The foreign key identifies a column or a set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table.”
</p>
</blockquote>
<p>
	Now that we’ve executed these queries, we have a pretty decent data model, but the relations are still missing. Let’s start with the definition of a relationship. We’re going to use something called a foreign key. A foreign key is a referential constraint between two tables. The foreign key identifies a column or a set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table. Some conditions:
	</p>
<ul>
<li>The column in the referencing table the foreign key refers to must be a primary key</li>
<li>The values that are referred to must exist in the referenced table</li>
</ul>
<p>
	By adding foreign keys the information is linked together which is very important for database normalization. Now you know what a foreign key is and why we’re using them. It’s time to add them to the tables we’ve already made by using the ALTER statement, which can be used to change an already existing table.
	</p>
<p>
	We’ll link the topics to the categories first:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code158'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141158"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code158"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> topics <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>topic_cat<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> categories<span style="color: #66cc66;">&#40;</span>cat_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> CASCADE <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CASCADE;</pre></td></tr></table></div>

<p>
	The last part of the query already says what happens. When a category gets deleted from the database, all the topics will be deleted too. If the cat_id of a category changes, every topic will be updated too. That’s what the ON UPDATE CASCADE part is for. Of course, you can reverse this to protect your data, so that you can’t delete a category as long as it still has topics linked to it. If you would want to do that, you could replace the ‘ON DELETE CASCADE’ part with ‘ON DELETE RESTRICT’. There is also SET NULL and NO ACTION, which speak for themselves.
	</p>
<p>
	Every topic is linked to a category now. Let’s link the topics to the user who creates one.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code159'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141159"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code159"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> topics <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>topic_by<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> users<span style="color: #66cc66;">&#40;</span>user_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> RESTRICT <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CASCADE;</pre></td></tr></table></div>

<p>
	This foreign key is the same as the previous one, but there is one difference: the user can’t be deleted as long as there are still topics with the user id of the user. We don’t use CASCADE here because there might be valuable information in our topics. We wouldn’t want that information to get deleted if someone decides to delete their account. To still give users the opportunity to delete their account, you could build some feature that anonymizes all their topics and then delete their account. Unfortunately, that is beyond the scope of this tutorial.
	</p>
<p><strong><br />
	Link the posts to the topics:<br />
	</strong></p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code160'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141160"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code160"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> posts <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>post_topic<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> topics<span style="color: #66cc66;">&#40;</span>topic_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> CASCADE <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CASCADE;</pre></td></tr></table></div>

<p>
	<strong>And finally, link each post to the user who made it:</strong>
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code161'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141161"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code161"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> posts <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>post_by<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> users<span style="color: #66cc66;">&#40;</span>user_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> RESTRICT <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CASCADE;</pre></td></tr></table></div>

<p>
	That’s the database part! It was quite a lot of work, but the result, a great data model, is definitely worth it.
</p>
<p></p>
<h2><span>Step 2</span>: Introduction to the Header/Footer System</h2>
<p>
	Each page of our forum needs a few basic things, like a doctype and some markup. That’s why we’ll include a header.php file at the top of each page, and a footer.php at the bottom. The header.php contains a doctype, a link to the stylesheet and some important information about the forum, such as the title tag and metatags.
	</p>
<h5>header.php</h5>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code162'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141162"><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
</pre></td><td class="code" id="p141code162"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;</span>
<span style="color: #0000ff;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html xmlns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml<span style="color: #339933;">:</span>lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nl&quot;</span> lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nl&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #339933;">/&gt;</span>
	<span style="color: #339933;">&lt;</span>meta name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;description&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;A short description.&quot;</span> <span style="color: #339933;">/&gt;</span>
	<span style="color: #339933;">&lt;</span>meta name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;keywords&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;put, keywords, here&quot;</span> <span style="color: #339933;">/&gt;</span>
	<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>PHP<span style="color: #339933;">-</span><a href="http://www.php.net/mysql"><span style="color: #990000;">MySQL</span></a> forum<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;style.css&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>My forum<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrapper&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;menu&quot;</span><span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/forum/index.php&quot;</span><span style="color: #339933;">&gt;</span>Home<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span>
		<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/forum/create_topic.php&quot;</span><span style="color: #339933;">&gt;</span>Create a topic<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span>
		<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/forum/create_cat.php&quot;</span><span style="color: #339933;">&gt;</span>Create a category<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
&nbsp;
		<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userbar&quot;</span><span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userbar&quot;</span><span style="color: #339933;">&gt;</span>Hello Example<span style="color: #339933;">.</span> Not you? <a href="http://www.php.net/log"><span style="color: #990000;">Log</span></a> out<span style="color: #339933;">.&lt;/</span>div<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;content&quot;</span><span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>
		The wrapper div will be used to make it easier to style the entire page. The menu div obviously contains a menu with links to pages we still have to create, but it helps to see where we’re going a little bit. The userbar div is going to be used for a small top bar which contains some information like the username and a link to the logout page. The content page holds the actual content of the page, obviously.
	</p>
<p>
		The attentive reader might have already noticed we’re missing some things. There is no </body> or </html> tag. They’re in the footer.php page, as you can see below.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code163'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141163"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p141code163"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;&lt;!--</span> content <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;&lt;!--</span> wrapper <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;footer&quot;</span><span style="color: #339933;">&gt;</span>Created <span style="color: #b1b100;">for</span> GigaSpartan<span style="color: #339933;">.</span>com<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>
		When we include a header and a footer on each page the rest of the page get embedded between the header and the footer. This method has got some advantages. First and foremost, everything will be styled correctly. A short example:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code164'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141164"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p141code164"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 	<span style="color: #666666; font-style: italic;">//the beautifully styled content, everything looks good</span>
 	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div id=&quot;content&quot;&gt;some text&lt;/div&gt;'</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;">//bad looking, unstyled error :-(</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		As you can see, a page without errors will result in a nice page with the content. But if there’s an error, everything looks really ugly; so that’s why it’s better to make sure not only real content is styled correctly, but also the errors we might get.
	</p>
<p>
		Another advantage is the possibility of making quick changes. You can see for yourself by editing the text in footer.php when you’ve finished this tutorial; you’ll notice that the footer changes on every page immediately. Finally, we add a stylesheet which provides us with some basic markup – nothing too fancy.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code165'); return false;">View Code</a> CSS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141165"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
</pre></td><td class="code" id="p141code165"><pre class="css" style="font-family:monospace;">body <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#4E4E4E</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>			<span style="color: #808080; font-style: italic;">/* make sure IE centers the page too */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#wrapper</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">900px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span> 			<span style="color: #808080; font-style: italic;">/* center the page */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#content</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>				<span style="color: #808080; font-style: italic;">/* fill up the entire div */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#menu</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>		<span style="color: #808080; font-style: italic;">/* avoid a double border */</span>
	<span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">both</span><span style="color: #00AA00;">;</span>				<span style="color: #808080; font-style: italic;">/* clear:both makes sure the content div doesn't float next to this one but stays under it */</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">85%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#menu</span> a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#009FC1</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#userbar</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">250px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">both</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* begin table styles */</span>
table <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border-collapse</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">collapse</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
table a <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
table a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#373737</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
th <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#B40E1F</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#F0F0F0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
td <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Begin font styles */</span>
h1<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#F1F3F1</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
h3 <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Menu styles */</span>
<span style="color: #6666ff;">.item</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#00728B</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#032472</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.leftpart</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">70%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.rightpart</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.small</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#373737</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">65%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.topic-post</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
.post-<span style="color: #000000; font-weight: bold;">content</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
textarea <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p></p>
<h2><span>Step 3</span>: Getting Ready for Action</h2>
<p>
		Before we can read anything from our database, we need a connection. That’s what connect.php is for. We’ll include it in every file we are going to create.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code166'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141166"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p141code166"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//connect.php</span>
<span style="color: #000088;">$server</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'usernamehere'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'passwordhere'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'databasenamehere'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$server</span><span style="color: #339933;">,</span> <span style="color: #000088;">$username</span><span style="color: #339933;">,</span>  <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 	<a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error: could not establish database connection'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/mysql_select_db"><span style="color: #990000;">mysql_select_db</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 	<a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error: could not select the database'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		Simply replace the default values of the variables at the top of the page with your own date, save the file and you’re good to go!
	</p>
<p></p>
<h2><span>Step 4</span>: Displaying the Forum Overview</h2>
<p>
		Since we’re just started with some basic techniques, we’re going to make a simplified version of the forum overview for now.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code167'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141167"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p141code167"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;tr&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;leftpart&quot;&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h3&gt;&lt;a href=&quot;category.php?id=&quot;&gt;Category name&lt;/a&gt;&lt;/h3&gt; Category description goes here'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;rightpart&quot;&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;a href=&quot;topic.php?id=&quot;&gt;Topic subject&lt;/a&gt; at 10-10'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/tr&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		There you have it: a nice and clean overview. We’ll be updating this page throughout the tutorial so that it becomes more like the end result, step by step!
	</p>
<p></p>
<h2><span>Step 5</span>: Signing up a User</h2>
<p>
		Let’s start by making a simple HTML form so that a new user can register.
	</p>
<p>
		A PHP page is needed to process the form. We’re going to use a $_SERVER variable. The $_SERVER variable is an array with values that are automatically set with each request. One of the values of the $_SERVER array is ‘REQUEST_METHOD’. When a page is requested with GET, this variable will hold the value ‘GET’. When a page is requested via POST, it will hold the value ‘POST’. We can use this value to check if a form has been posted. See the signup.php page below.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code168'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141168"><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
</pre></td><td class="code" id="p141code168"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//signup.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h3&gt;Sign up&lt;/h3&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/*the form hasn't been posted yet, display it
	  note that the action=&quot;&quot; will cause the form to post to the same page it is on */</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
 	 	Username: &lt;input type=&quot;text&quot; name=&quot;user_name&quot; /&gt;
 		Password: &lt;input type=&quot;password&quot; name=&quot;user_pass&quot;&gt;
		Password again: &lt;input type=&quot;password&quot; name=&quot;user_pass_check&quot;&gt;
		E-mail: &lt;input type=&quot;email&quot; name=&quot;user_email&quot;&gt;
 		&lt;input type=&quot;submit&quot; value=&quot;Add category&quot; /&gt;
 	 &lt;/form&gt;'</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;">/* so, the form has been posted, we'll process the data in three steps:
		1.	Check the data
		2.	Let the user refill the wrong fields (if necessary)
		3.	Save the data
	*/</span>
	<span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* declare the array for later use */</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</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: #666666; font-style: italic;">//the user name exists</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/ctype_alnum"><span style="color: #990000;">ctype_alnum</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</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;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The username can only contain letters and digits.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The username cannot be longer than 30 characters.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The username field must not be empty.'</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><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass_check'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The two passwords did not match.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The password field cannot be empty.'</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*check for an empty array, if there are errors, they're in this array (note the ! operator)*/</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Uh-oh.. a couple of fields are not filled in correctly..'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/* walk through the array so all the errors get displayed */</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* this generates a nice error list */</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ul&gt;'</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;">//the form has been posted without, so save it</span>
		<span style="color: #666666; font-style: italic;">//notice the use of mysql_real_escape_string, keep everything safe!</span>
		<span style="color: #666666; font-style: italic;">//also notice the sha1 function which hashes the password</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO
					users(user_name, user_pass, user_email ,user_date, user_level)
				VALUES('&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
					   '&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/sha1"><span style="color: #990000;">sha1</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
					   '&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
						NOW(),
						0)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//something went wrong, display the error</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Something went wrong while registering. Please try again later.'</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//echo mysql_error(); //debugging purposes, uncomment when needed</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Successfully registered. You can now &lt;a href=&quot;signin.php&quot;&gt;sign in&lt;/a&gt; and start posting! :-)'</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;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		A lot of explanation is in the comments I made in the file, so be sure to check them out. The processing of the data takes place in three parts:
	</p>
<ul>
<li>Validating the data</li>
<li>If the data is not valid, show the form again</li>
<li>If the data is valid, save the record in the database</li>
</ul>
<p>
		The PHP part is quite self-explanatory. The SQL-query however probably needs a little more explanation.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code169'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141169"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p141code169"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span>
       users<span style="color: #66cc66;">&#40;</span>user_name<span style="color: #66cc66;">,</span> user_pass<span style="color: #66cc66;">,</span> user_email <span style="color: #66cc66;">,</span>user_date<span style="color: #66cc66;">,</span> user_level<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&quot; . mysql_real_escape_string($_POST['</span>user_name<span style="color: #ff0000;">']) . &quot;'</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">'&quot; . sha1($_POST['</span>user_pass<span style="color: #ff0000;">']) . &quot;'</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">'&quot; . mysql_real_escape_string($_POST['</span>user_email<span style="color: #ff0000;">']) . &quot;'</span><span style="color: #66cc66;">,</span>
       NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
       <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>
		On line 1 we have the INSERT INTO statement which speaks for itself. The table name is specified on the second line. The words between the brackets represent the columns in which we want to insert the data. The VALUES statement tells the database we’re done declaring column names and it’s time to specify the values. There is something new here: mysql_real_escape_string. The function escapes special characters in an unescaped string , so that it is safe to place it in a query. This function MUST always be used, with very few exceptions. There are too many scripts that don’t use it and can be hacked real easy. Don’t take the risk, use mysql_real_escape_string().
	</p>
<blockquote class="pullquote pqRight">
<p>
“Never insert a plain password as-is. You MUST always encrypt it.”
</p>
</blockquote>
<p>
		Also, you can see that the function sha1() is used to encrypt the user’s password. This is also a very important thing to remember. Never insert a plain password as-is. You MUST always encrypt it. Imagine a hacker who somehow manages to get access to your database. If he sees all the plain-text passwords he could log into any (admin) account he wants. If the password columns contain sha1 strings he has to crack them first which is almost impossible.
	</p>
<p>
		Note: it’s also possible to use md5(), I always use sha1() because benchmarks have proved it’s a tiny bit faster, not much though. You can replace sha1 with md5 if you like.
	</p>
<p>
		If the signup process was successful, you should see something like this:
	</p>
<p>
		Try refreshing your phpMyAdmin screen, a new record should be visible in the users table.
	</p>
<p></p>
<h2><span>Step 6</span>: Adding Authentication and User Levels</h2>
<p>
		An important aspect of a forum is the difference between regular users and admins/moderators. Since this is a small forum and adding features like adding new moderators and stuff would take way too much time, we’ll focus on the login process and create some admin features like creating new categories and closing a thread.
	</p>
<p>
		Now that you’ve completed the previous step, we’re going to make your freshly created account an admin account. In phpMyAdmin, click on the users table, and then ‘Browse’. Your account will probably pop up right away. Click the edit icon and change the value of the user_level field from 0 to 1. That’s it for now. You won’t notice any difference in our application immediately, but when we’ve added the admin features a normal account and your account will have different capabilities.
	</p>
<p>
		The sign-in process works the following way:
	</p>
<ul>
<li>A visitor enters user data and submits the form</li>
<li>If the username and password are correct, we can start a session</li>
<li>If the username and password are incorrect, we show the form again with a message</li>
</ul>
<p>
		The signin.php file is below. Don’t think I’m not explaining what I’m doing, but check out the comments in the file. It’s much easier to understand that way.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code170'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141170"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
</pre></td><td class="code" id="p141code170"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//signin.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h3&gt;Sign in&lt;/h3&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//first, check if the user is already signed in. If that is the case, there is no need to display this page</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</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: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</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;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You are already signed in, you can &lt;a href=&quot;signout.php&quot;&gt;sign out&lt;/a&gt; if you want.'</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">/*the form hasn't been posted yet, display it
		  note that the action=&quot;&quot; will cause the form to post to the same page it is on */</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
			Username: &lt;input type=&quot;text&quot; name=&quot;user_name&quot; /&gt;
			Password: &lt;input type=&quot;password&quot; name=&quot;user_pass&quot;&gt;
			&lt;input type=&quot;submit&quot; value=&quot;Sign in&quot; /&gt;
		 &lt;/form&gt;'</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;">/* so, the form has been posted, we'll process the data in three steps:
			1.	Check the data
			2.	Let the user refill the wrong fields (if necessary)
			3.	Varify if the data is correct and return the correct response
		*/</span>
		<span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* declare the array for later use */</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</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;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The username field must not be empty.'</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: #339933;">!</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</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;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The password field must not be empty.'</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*check for an empty array, if there are errors, they're in this array (note the ! operator)*/</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Uh-oh.. a couple of fields are not filled in correctly..'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/* walk through the array so all the errors get displayed */</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* this generates a nice error list */</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ul&gt;'</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;">//the form has been posted without errors, so save it</span>
			<span style="color: #666666; font-style: italic;">//notice the use of mysql_real_escape_string, keep everything safe!</span>
			<span style="color: #666666; font-style: italic;">//also notice the sha1 function which hashes the password</span>
			<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
						user_id,
						user_name,
						user_level
					FROM
						users
					WHERE
						user_name = '&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'
					AND
						user_pass = '&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/sha1"><span style="color: #990000;">sha1</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//something went wrong, display the error</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Something went wrong while signing in. Please try again later.'</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">//echo mysql_error(); //debugging purposes, uncomment when needed</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;">//the query was successfully executed, there are 2 possibilities</span>
				<span style="color: #666666; font-style: italic;">//1. the query returned data, the user can be signed in</span>
				<span style="color: #666666; font-style: italic;">//2. the query returned an empty result set, the credentials were wrong</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You have supplied a wrong user/password combination. Please try again.'</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;">//set the $_SESSION['signed_in'] variable to TRUE</span>
					<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #666666; font-style: italic;">//we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages</span>
					<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#123;</span>
						<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> 	<span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
						<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span> 	<span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
						<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_level'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_level'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
&nbsp;
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Welcome, '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'. &lt;a href=&quot;index.php&quot;&gt;Proceed to the forum overview&lt;/a&gt;.'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		This is the query that’s in the signin.php file:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code171'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141171"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p141code171"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
	user_id<span style="color: #66cc66;">,</span>
	user_name<span style="color: #66cc66;">,</span>
	user_level
<span style="color: #993333; font-weight: bold;">FROM</span>
	users
<span style="color: #993333; font-weight: bold;">WHERE</span>
	user_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'&quot; . mysql_real_escape_string($_POST['</span>user_name<span style="color: #ff0000;">']) . &quot;'</span>
<span style="color: #993333; font-weight: bold;">AND</span>
	user_pass <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'&quot; . sha1($_POST['</span>user_pass<span style="color: #ff0000;">'])</span></pre></td></tr></table></div>

<p>
		It’s obvious we need a check to tell if the supplied credentials belong to an existing user. A lot of scripts retrieve the password from the database and compare it using PHP. If we do this directly via SQL the password will be stored in the database once during registration and never leave it again. This is safer, because all the real action happens in the database layer and not in our application.
	</p>
<p>
		If the user is signed in successfully, we’re doing a few things:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code172'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141172"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p141code172"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//set the $_SESSION['signed_in'] variable to TRUE</span>
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 	<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
 	<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		First, we set the ‘signed_in’ $_SESSION var to true, so we can use it on other pages to make sure the user is signed in. We also put the username and user id in the $_SESSION variable for usage on a different page. Finally, we display a link to the forum overview so the user can get started right away.
	</p>
<p>
		Of course signing in requires another function, signing out! The sign-out process is actually a lot easier than the sign-in process. Because all the information about the user is stored in $_SESSION variables, all we have to do is unset them and display a message.
	</p>
<p>
		Now that we’ve set the $_SESSION variables, we can determine if someone is signed in. Let’s make a last simple change to header.php:
	</p>
<p>
		Replace:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code173'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141173"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code173"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;userbar&quot;&gt;Hello Example. Not you? Log out.&lt;/div&gt;</pre></td></tr></table></div>

<p>
		With:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code174'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141174"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p141code174"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userbar&quot;</span><span style="color: #339933;">&gt;</span>
 	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</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: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'. Not you? &lt;a href=&quot;signout.php&quot;&gt;Sign out&lt;/a&gt;'</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: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;a href=&quot;signin.php&quot;&gt;Sign in&lt;/a&gt; or &lt;a href=&quot;sign up&quot;&gt;create an account&lt;/a&gt;.'</span><span style="color: #339933;">;</span>
 	<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>
		If a user is signed in, he will see his or her name displayed on the front page with a link to the signout page. Our authentication is done! By now our forum should look like this:
	</p>
<p></p>
<h2><span>Step 7</span>: Creating a Category</h2>
<p>
		We want to create categories so let’s start with making a form.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code175'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141175"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p141code175"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
 	Category name: &lt;input type=&quot;text&quot; name=&quot;cat_name&quot; /&gt;
 	Category description: &lt;textarea name=&quot;cat_description&quot; /&gt;&lt;/textarea&gt;
	&lt;input type=&quot;submit&quot; value=&quot;Add category&quot; /&gt;
 &lt;/form&gt;</pre></td></tr></table></div>

<p>
		This step looks a lot like Step 4 (Signing up a user’), so I’m not going to do an in-depth explanation here. If you followed all the steps you should be able to understand this somewhat quickly.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code176'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141176"><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
</pre></td><td class="code" id="p141code176"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//the form hasn't been posted yet, display it</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;form method='</span>post<span style="color: #0000ff;">' action='</span><span style="color: #0000ff;">'&gt;
 	 	Category name: &lt;input type='</span>text<span style="color: #0000ff;">' name='</span>cat_name<span style="color: #0000ff;">' /&gt;
 		Category description: &lt;textarea name='</span>cat_description<span style="color: #0000ff;">' /&gt;&lt;/textarea&gt;
 		&lt;input type='</span>submit<span style="color: #0000ff;">' value='</span>Add category<span style="color: #0000ff;">' /&gt;
 	 &lt;/form&gt;'</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;">//the form has been posted, so save it</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> ìINSERT INTO categories<span style="color: #009900;">&#40;</span>cat_name<span style="color: #339933;">,</span> cat_description<span style="color: #009900;">&#41;</span>
 	   VALUES<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_name'</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: #0000ff;">' . mysql_real_escape_string($_POST['</span>cat_description<span style="color: #0000ff;">']) . ì'</span><span style="color: #009900;">&#41;</span><span style="color: #0000ff;">';
    $result = mysql_query($sql);
    if(!$result)
    {
        //something went wrong, display the error
        echo '</span>Error<span style="color: #0000ff;">' . mysql_error();
    }
    else
    {
        echo '</span><span style="color: #000000; font-weight: bold;">New</span> category successfully added<span style="color: #339933;">.</span><span style="color: #0000ff;">';
    }
}
?&gt;</span></pre></td></tr></table></div>

<p>
		As you can see, we’ve started the script with the $_SERVER check, after checking if the user has admin rights, which is required for creating a category. The form gets displayed if it hasn’t been submitted already. If it has, the values are saved. Once again, a SQL query is prepared and then executed.
	</p>
<p></p>
<h2><span>Step 8</span>: Adding Categories to index.php</h2>
<p>
		We’ve created some categories, so now we’re able to display them on the front page. Let’s add the following query to the content area of index.php.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code177'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141177"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p141code177"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
 	categories<span style="color: #66cc66;">.</span>cat_id<span style="color: #66cc66;">,</span>
	categories<span style="color: #66cc66;">.</span>cat_name<span style="color: #66cc66;">,</span>
 	categories<span style="color: #66cc66;">.</span>cat_description<span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">FROM</span>
 	categories</pre></td></tr></table></div>

<p>
		This query selects all categories and their names and descriptions from the categories table. We only need a bit of PHP to display the results. If we add that part just like we did in the previous steps, the code will look like this.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code178'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141178"><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
</pre></td><td class="code" id="p141code178"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
			cat_id,
			cat_name,
			cat_description,
		FROM
			categories&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The categories could not be displayed, please try again later.'</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No categories defined yet.'</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;">//prepare the table</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;table border=&quot;1&quot;&gt;
			  &lt;tr&gt;
				&lt;th&gt;Category&lt;/th&gt;
				&lt;th&gt;Last topic&lt;/th&gt;
			  &lt;/tr&gt;'</span><span style="color: #339933;">;</span>	
&nbsp;
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;tr&gt;'</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;leftpart&quot;&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h3&gt;&lt;a href=&quot;category.php?id&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/a&gt;&lt;/h3&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_description'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;rightpart&quot;&gt;'</span><span style="color: #339933;">;</span>
							<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;a href=&quot;topic.php?id=&quot;&gt;Topic subject&lt;/a&gt; at 10-10'</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/tr&gt;'</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;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		Notice how we’re using the cat_id to create links to category.php. All the links to this page will look like this: category.php?cat_id=x, where x can be any numeric value. This may be new to you. We can check the url with PHP for $_GET values. For example, we have this link:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code179'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141179"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code179"><pre class="html" style="font-family:monospace;">category.php?cat_id=23</pre></td></tr></table></div>

<p>
		The statement echo $_GET[ëcat_id'];’ will display ’23′. In the next few steps we’ll use this value to retrieve the topics when viewing a single category, but topics can’t be viewed if we haven’t created them yet. So let’s create some topics!
	</p>
<p></p>
<h2><span>Step 9</span>: Creating a Topic</h2>
<p>
		In this step, we’re combining the techniques we learned in the previous steps. We’re checking if a user is signed in, we’ll use an input query to create the topic and create some basic HTML forms.
	</p>
<p>
		The structure of create_topic.php can hardly be explained in a list or something, so I rewrote it in pseudo-code.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code180'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141180"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p141code180"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>user is signed in<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//the user is not signed in</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;">//the user is signed in</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>form has not been posted<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//show form</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;">//process form</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		Here’s the real code of this part of our forum, check the explanations below the code to see what it’s doing.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code181'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141181"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
</pre></td><td class="code" id="p141code181"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h2&gt;Create a topic&lt;/h2&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//the user is not signed in</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Sorry, you have to be &lt;a href=&quot;/forum/signin.php&quot;&gt;signed in&lt;/a&gt; to create a topic.'</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;">//the user is signed in</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//the form hasn't been posted yet, display it</span>
		<span style="color: #666666; font-style: italic;">//retrieve the categories from the database for use in the dropdown</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
					cat_id,
					cat_name,
					cat_description
				FROM
					categories&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//the query failed, uh-oh :-(</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Error while selecting from database. Please try again later.'</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//there are no categories, so a topic can't be posted</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_level'</span><span style="color: #009900;">&#93;</span> <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: #b1b100;">echo</span> <span style="color: #0000ff;">'You have not created categories yet.'</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: #b1b100;">echo</span> <span style="color: #0000ff;">'Before you can post a topic, you must wait for an admin to create some categories.'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #009900;">&#123;</span>
&nbsp;
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
					Subject: &lt;input type=&quot;text&quot; name=&quot;topic_subject&quot; /&gt;
					Category:'</span><span style="color: #339933;">;</span> 
&nbsp;
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;select name=&quot;topic_cat&quot;&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#123;</span>
						<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;option value=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/option&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/select&gt;'</span><span style="color: #339933;">;</span>	
&nbsp;
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message: &lt;textarea name=&quot;post_content&quot; /&gt;&lt;/textarea&gt;
					&lt;input type=&quot;submit&quot; value=&quot;Create topic&quot; /&gt;
				 &lt;/form&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//start the transaction</span>
		<span style="color: #000088;">$query</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;BEGIN WORK;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//Damn! the query failed, quit</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'An error occured while creating your topic. Please try again later.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//the form has been posted, so save it</span>
			<span style="color: #666666; font-style: italic;">//insert the topic into the topics table first, then we'll save the post into the posts table</span>
			<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO
						topics(topic_subject,
							   topic_date,
							   topic_cat,
							   topic_by)
				   VALUES('&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'topic_subject'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
							   NOW(),
							   &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'topic_cat'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;,
							   &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;
							   )&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//something went wrong, display the error</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'An error occured while inserting your data. Please try again later.'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ROLLBACK;&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//the first query worked, now start the second, posts query</span>
				<span style="color: #666666; font-style: italic;">//retrieve the id of the freshly created topic for usage in the posts query</span>
				<span style="color: #000088;">$topicid</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_insert_id"><span style="color: #990000;">mysql_insert_id</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO
							posts(post_content,
								  post_date,
								  post_topic,
								  post_by)
						VALUES
							('&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'post_content'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
								  NOW(),
								  &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$topicid</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;,
								  &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;
							)&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">//something went wrong, display the error</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'An error occured while inserting your post. Please try again later.'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ROLLBACK;&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;COMMIT;&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #666666; font-style: italic;">//after a lot of work, the query succeeded!</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You have successfully created &lt;a href=&quot;topic.php?id='</span><span style="color: #339933;">.</span> <span style="color: #000088;">$topicid</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;your new topic&lt;/a&gt;.'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		I’ll discuss this page in two parts, showing the form and processing the form.
	</p>
<p>
	<strong>Showing the form</strong><br />
	We’re starting with a simple HTML form. There is actually something special here, because we use a dropdown. This dropdown is filled with data from the database, using this query:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code182'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141182"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p141code182"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
 	cat_id<span style="color: #66cc66;">,</span>
 	cat_name<span style="color: #66cc66;">,</span>
 	cat_description
<span style="color: #993333; font-weight: bold;">FROM</span>
 	categories</pre></td></tr></table></div>

<p>
		That’s the only potentially confusing part here; it’s quite a piece of code, as you can see when looking at the create_topic.php file at the bottom of this step.
	</p>
<p>	<strong>Processing the form</strong></p>
<p>
		The process of saving the topic consists of two parts: saving the topic in the topics table and saving the first post in the posts table. This requires something quite advanced that goes a bit beyond the scope of this tutorial. It’s called a transaction, which basically means that we start by executing the start command and then rollback when there are database errors and commit when everything went well.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code183'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141183"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p141code183"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//start the transaction</span>
<span style="color: #000088;">$query</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;BEGIN WORK;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//stop the transaction</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ROLLBACK;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//commit the transaction</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;COMMIT;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		The first query being used to save the data is the topic creation query, which looks like this:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code184'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141184"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p141code184"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span>
	topics<span style="color: #66cc66;">&#40;</span>topic_subject<span style="color: #66cc66;">,</span>
               topic_date<span style="color: #66cc66;">,</span>
               topic_cat<span style="color: #66cc66;">,</span>
               topic_by<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&quot; . mysql_real_escape_string($_POST['</span>topic_subject<span style="color: #ff0000;">']) . &quot;'</span><span style="color: #66cc66;">,</span>
       NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">&quot; . mysql_real_escape_string($_POST['topic_cat']) . &quot;</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">&quot; . $_SESSION['user_id'] . &quot;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>
		At first the fields are defined, then the values to be inserted. We’ve seen the first one before, it’s just a string which is made safe by using mysql_real_escape_string(). The second value, NOW(), is a SQL function for the current time. The third value, however, is a value we haven’t seen before. It refers to a (valid) id of a category. The last value refers to an (existing) user_id which is, in this case, the value of $_SESSION[ëuser_id']. This variable was declared during the sign in process.
	</p>
<p>
		If the query executed without errors we proceed to the second query. Remember we are still doing a transaction here. If we would’ve got errors we would have used the ROLLBACK command.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code185'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141185"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p141code185"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span>
        posts<span style="color: #66cc66;">&#40;</span>post_content<span style="color: #66cc66;">,</span>
        post_date<span style="color: #66cc66;">,</span>
        post_topic<span style="color: #66cc66;">,</span>
        post_by<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">VALUES</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&quot; . mysql_real_escape_string($_POST['</span>post_content<span style="color: #ff0000;">']) . &quot;'</span><span style="color: #66cc66;">,</span>
         NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
         <span style="color: #ff0000;">&quot; . $topicid . &quot;</span><span style="color: #66cc66;">,</span>
         <span style="color: #ff0000;">&quot; . $_SESSION['user_id'] . &quot;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>
		The first thing we do in this code is use mysql_insert_id() to retrieve the latest generated id from the topic_id field in the topics table. As you may remember from the first steps of this tutorial, the id is generated in the database using auto_increment.
	</p>
<p>
		Then the post is inserted into the posts table. This query looks a lot like the topics query. The only difference is that this post refers to the topic and the topic referred to a category. From the start, we decided to create a good data model and here is the result: a nice hierarchical structure.
	</p>
<p></p>
<h2><span>Step 10</span>: Category View</h2>
<p>
		We’re going to make an overview page for a single category. We’ve just created a category, it would be handy to be able to view all the topics in it. First, create a page called category.php.
	</p>
<p>
		A short list of the things we need:
	</p>
<h4>Needed for displaying the category</h4>
<ul>
<li>cat_name</li>
<li>cat_description</li>
</ul>
<p>	<i>Needed for displaying all the topics</i></p>
<ul>
<li>topic_id</li>
<li>topic_subject</li>
<li>topic_date</li>
<li>topic_cat</li>
</ul>
<p>
		Let’s create the two SQL queries that retrieve exactly this data from the database.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code186'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141186"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p141code186"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
    cat_id<span style="color: #66cc66;">,</span>
    cat_name<span style="color: #66cc66;">,</span>
    cat_description
<span style="color: #993333; font-weight: bold;">FROM</span>
    categories
<span style="color: #993333; font-weight: bold;">WHERE</span>
    cat_id <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; . mysql_real_escape_string($_GET['id'])</span></pre></td></tr></table></div>

<p>
		The query above selects all the categories from the database.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code187'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141187"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p141code187"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
    topic_id<span style="color: #66cc66;">,</span>
    topic_subject<span style="color: #66cc66;">,</span>
    topic_date<span style="color: #66cc66;">,</span>
    topic_cat
<span style="color: #993333; font-weight: bold;">FROM</span>
    topics
<span style="color: #993333; font-weight: bold;">WHERE</span>
    topic_cat <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; . mysql_real_escape_string($_GET['id'])</span></pre></td></tr></table></div>

<p>
		The query above is executed in the while loop in which we echo the categories. By doing it this way, we’ll see all the categories and the latest topic for each of them.<br />
		The complete code of category.php will be the following:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code188'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141188"><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
</pre></td><td class="code" id="p141code188"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//first select the category based on $_GET['cat_id']</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
			cat_id,
			cat_name,
			cat_description
		FROM
			categories
		WHERE
			cat_id = &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The category could not be displayed, please try again later.'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'This category does not exist.'</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;">//display category data</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h2&gt;Topics in ′'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'′ category&lt;/h2&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//do a query for the topics</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
					topic_id,
					topic_subject,
					topic_date,
					topic_cat
				FROM
					topics
				WHERE
					topic_cat = &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The topics could not be displayed, please try again later.'</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'There are no topics in this category yet.'</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;">//prepare the table</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;table border=&quot;1&quot;&gt;
					  &lt;tr&gt;
						&lt;th&gt;Topic&lt;/th&gt;
						&lt;th&gt;Created at&lt;/th&gt;
					  &lt;/tr&gt;'</span><span style="color: #339933;">;</span>	
&nbsp;
				<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;tr&gt;'</span><span style="color: #339933;">;</span>
						<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;leftpart&quot;&gt;'</span><span style="color: #339933;">;</span>
							<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h3&gt;&lt;a href=&quot;topic.php?id='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'topic_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'topic_subject'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/a&gt;&lt;h3&gt;'</span><span style="color: #339933;">;</span>
						<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
						<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;rightpart&quot;&gt;'</span><span style="color: #339933;">;</span>
							<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d-m-Y'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/strtotime"><span style="color: #990000;">strtotime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'topic_date'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/tr&gt;'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		And here is the final result of our categories page:
	</p>
<p></p>
<h2><span>Step 11</span>: Topic View</h2>
<p>
		The SQL queries in this step are complicated ones. The PHP-part is all stuff that you’ve seen before. Let’s take a look at the queries. The first one retrieves basic information about the topic:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code189'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141189"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p141code189"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
    topic_id<span style="color: #66cc66;">,</span>
    topic_subject
<span style="color: #993333; font-weight: bold;">FROM</span>
    topics
<span style="color: #993333; font-weight: bold;">WHERE</span>
    topics<span style="color: #66cc66;">.</span>topic_id <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; . mysql_real_escape_string($_GET['id'])</span></pre></td></tr></table></div>

<p>
		This information is displayed in the head of the table we will use to display all the data. Next, we retrieve all the posts in this topic from the database. The following query gives us exactly what we need:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code190'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141190"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p141code190"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
    posts<span style="color: #66cc66;">.</span>post_topic<span style="color: #66cc66;">,</span>
    posts<span style="color: #66cc66;">.</span>post_content<span style="color: #66cc66;">,</span>
    posts<span style="color: #66cc66;">.</span>post_date<span style="color: #66cc66;">,</span>
    posts<span style="color: #66cc66;">.</span>post_by<span style="color: #66cc66;">,</span>
    users<span style="color: #66cc66;">.</span>user_id<span style="color: #66cc66;">,</span>
    users<span style="color: #66cc66;">.</span>user_name
<span style="color: #993333; font-weight: bold;">FROM</span>
    posts
<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span>
    users
<span style="color: #993333; font-weight: bold;">ON</span>
    posts<span style="color: #66cc66;">.</span>post_by <span style="color: #66cc66;">=</span> users<span style="color: #66cc66;">.</span>user_id
<span style="color: #993333; font-weight: bold;">WHERE</span>
    posts<span style="color: #66cc66;">.</span>post_topic <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; . mysql_real_escape_string($_GET['id'])</span></pre></td></tr></table></div>

<p>
		This time, we want information from the users and the posts table – so we use the LEFT JOIN again. The condition is: the user id should be the same as the post_by field. This way we can show the username of the user who replied at each post.</p>
<p>
		The final topic view looks like this:
	</p>
<p></p>
<h2><span>Step 12</span>: Adding a Reply</h2>
<p>
		Let’s create the last missing part of this forum, the possibility to add a reply. We’ll start by creating a form:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code191'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141191"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p141code191"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;reply.php?id=5&quot;&gt;
    &lt;textarea name=&quot;reply-content&quot;&gt;&lt;/textarea&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Submit reply&quot; /&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>
		The complete reply.php code looks like this.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code192'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141192"><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
</pre></td><td class="code" id="p141code192"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//someone is calling the file directly, which we don't want</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'This file cannot be called directly.'</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;">//check for sign in status</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You must be signed in to post a reply.'</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;">//a real user posted a real reply</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO
					posts(post_content,
						  post_date,
						  post_topic,
						  post_by)
				VALUES ('&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'reply-content'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
						NOW(),
						&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;,
						&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Your reply has not been saved, please try again later.'</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: #b1b100;">echo</span> <span style="color: #0000ff;">'Your reply has been saved, check out &lt;a href=&quot;topic.php?id='</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;the topic&lt;/a&gt;.'</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;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		The comments in the code pretty much detail what’s happening. We’re checking for a real user and then inserting the post into the database.
	</p>
<p></p>
<h2>Finishing Up</h2>
<p>
		Now that you’ve finished this tutorial, you should have a much better understanding of what it takes to build a forum. I hope my explanations were clear enough! Thanks again for reading.
	</p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2011/01/03/tutorial-create-a-phpmysql-powered-forum-from-scratch/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Tutorial: Simple Techniques to Lock Down your Website</title>
		<link>http://gigaspartan.com/2010/12/31/tutorial-simple-techniques-to-lock-down-your-website/</link>
		<comments>http://gigaspartan.com/2010/12/31/tutorial-simple-techniques-to-lock-down-your-website/#comments</comments>
		<pubDate>Fri, 31 Dec 2010 15:11:22 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[cryptology]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[Obfuscation]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[randomization]]></category>
		<category><![CDATA[secuirty]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=123</guid>
		<description><![CDATA[One crucial part of PHP development practice is always keeping in mind that security is not something you can simply buy off the shelf at your local convenient store. Ensuring the security of your web applications is a process, which over time, needs to be constantly evaluated, monitored, and hardened. Introduction While the use of [...]]]></description>
			<content:encoded><![CDATA[<p>One crucial part of PHP development practice is always keeping in mind that security is not something you can simply buy off the shelf at your local convenient store.  Ensuring the security of your web applications is a process, which over time, needs to be constantly evaluated, monitored, and hardened.</p>
<p><span id="more-7035"></span></p>
<p></p>
<h2>Introduction</h2>
<p>While the use of filters and validating data is one part of the security process, a web developer should be aware that Randomization, Obfuscation, and Cryptography in PHP can make a difference in the security of web applications.  This tutorial will guide you through some simple techniques at creating and using random or unique values within your web applications, taking a look and applying some general obfuscation techniques, and looking deeper into the science of Cryptology and it’s use within PHP.</p>
<p></p>
<h2>What you Will Learn</h2>
<ul>
<li>How to generate random values with PHP</li>
<li>Generating random Passwords</li>
<li>Salting Passwords and Authenticating The User</li>
<li>Obfuscation in PHP, an Overview</li>
<li>Cryptography in PHP and it’s Applications</li>
</ul>
<p><span id="more-123"></span><br />
</p>
<h2>Generating Random Values</h2>
<p>Dictionary.com defines randomization as:</p>
<blockquote><p>“-verb: to order or select in a random manner, as in a sample or experiment, especially in order to reduce bias and interference caused by irrelevant variables; make random.”</p>
</blockquote>
<p>Random number generation is determined in a variety of ways, however computational generators fall short of ‘true’ randomness as seen in nature or electronic noise(the fuzzy, screeching, black and white channel on TV).  These computed values are regarded as pseudo-random.</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/whitenoise.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/whitenoise.jpg" style="display: inline; "></div>
<p>PHP provides us with a couple of different ways to create random values. Let’s look at a few of the more popular functions.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code208'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123208"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p123code208"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<a href="http://www.php.net/rand"><span style="color: #990000;">rand</span></a><span style="color: #009900;">&#40;</span>int <span style="color: #000088;">$min</span><span style="color: #339933;">,</span> int <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span>int <span style="color: #000088;">$min</span><span style="color: #339933;">,</span> int <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/str_shuffle"><span style="color: #990000;">str_shuffle</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$prefix</span><span style="color: #339933;">,</span> more_entropy<span style="color: #339933;">=</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The two functions <code>rand()</code> and <code>mt_rand() </code>are likely the most widely used functions to generate a set of random numbers in PHP.  The function<code> rand()</code>; is an older generator, and is falling out of use due to <code>mt_rand()</code>; which is faster, more reliable, and can handle a higher maximum integer value on some platforms. The function <code>str_shuffle() </code>does exactly what you would expect it to, it shuffles a string passed to it.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code209'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123209"><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
</pre></td><td class="code" id="p123code209"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Examples of mt_rand() usage</span>
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//default</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//Outputs a random integer between 0 and 20</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Examples of rand() usage</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/rand"><span style="color: #990000;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//default</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/rand"><span style="color: #990000;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//Outputs a random integer between 0 and 25</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Example of str_shuffle usage</span>
&nbsp;
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcefghijklmnopqrstuvwxyz'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/str_shuffle"><span style="color: #990000;">str_shuffle</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//shuffles $string</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The <code>rand()</code> and <code>mt_rand()</code> functions both accept two parameters where <code>$min</code> is the lowest integer to start with, and <code>$max</code> being the largest integer to end with. The function <code>str_shuffle</code> takes one parameter, a string, outputting a shuffled mutation of the string. It acts the same as if you were shuffling a deck of cards.</p>
<p>While <code>mt_rand(); </code>will spit out a random integer, and <code>str_shuffle </code>will mix a string up, a function widely used to create random unique values is <code>uniqid()</code>.  This generates a prefixed unique identifier based on the current time in microseconds(<a href="http://us.php.net/manual/en/function.uniqid.php" title="PHP uniqid manual">via php.net</a>).</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code210'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123210"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p123code210"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Examples of uniqid() usage</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//default</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;GIGASPARTAN&quot;</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: #666666; font-style: italic;">//Adding an additional prefix and setting more_entropy to TRUE</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The function <code>uniqid()</code> accepts two parameters the first appends a prefix to the results while the second, if set to TRUE, will add additional <a href="http://en.wikipedia.org/wiki/Information_entropy" title="Wikipedia: Entropy">entropy</a> to the end of the returned value.</p>
<p></p>
<h2>Generating Random Passwords</h2>
<p>There are a gazillion examples on the web which generate random passwords, all do a fine job at it.  “But why,” you ask “would I need to generate a random password?”  Well the answer, quite simply, is so you do not have to rely on the end user to provide themselves with a less than secure password at the get go.  Generating random passwords is very useful in user registrations, or when a user makes a request because they have forgotten their password.  Doing this ensures a strong password at the beginning of a users experience at your website, or can cut down lines of code when a user needs to gain access again.</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/shuffle.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/shuffle.jpg" style="display: inline; "></div>
<p>Let’s look at some examples:Example 1</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code211'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123211"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p123code211"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//A simple function which will output a random password</span>
<span style="color: #000000; font-weight: bold;">function</span> randompassword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_shuffle"><span style="color: #990000;">str_shuffle</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#%$*'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">return</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pass</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</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: #666666; font-style: italic;">//returns the password</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This example shuffles a string with <code>str_shuffle</code> and will return a string within a counted range.  So if you wanted to generate an 8 character password then you would pass 8 to the function randompassword, or randompassword(8) from your source code.</p>
<p>Example 2</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code212'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123212"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p123code212"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Another example to create a random password</span>
<span style="color: #000000; font-weight: bold;">function</span> anorandpass<span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$m_rand</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//generate a random integer</span>
&nbsp;
	<span style="color: #000088;">$u_id</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MNO!@#$%^&amp;amp;*=+XYZ&quot;</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: #666666; font-style: italic;">//create a unique identifier with some extra prefix and extra entropy</span>
&nbsp;
	<span style="color: #000088;">$combine</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$m_rand</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$u_id</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Combine the variables to form a string</span>
&nbsp;
	<span style="color: #000088;">$new</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_shuffle"><span style="color: #990000;">str_shuffle</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combine</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//shuffle our string</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</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: #666666; font-style: italic;">//return the password</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">print</span> anorandpass<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In comparison, example one takes a static string and mixes it up then returns it, example two adds in more dynamic flavor(mmm tasty).  In example two the string being shuffled is no longer static, but changes with each generation.  While the first example is certainly sufficient in most cases to generate a strong password, the second example allows us to ensure the string length and characters will change with use, greatly decreasing the chance of a duplication.</p>
<p>Enforcing the use of strong passwords within a web application will deter users from visiting or signing up for a website.  It is often a trade off between getting the traffic you desire, and ensuring the security of the application.  I suggest allowing your users to create their own passwords at sign-up, or allow them to choose between the two.</p>
<p></p>
<h2>Please Pass the Salt.  Salting Passwords for Increased Security.</h2>
<p>Salting passwords is an effective way to increase the security of your users accounts even if an attacker gains access to your database, if done right.  It can be argued that, with access to the salt, an attacker can still gain your credentials.  While this is true, applying some randomization techniques to the storage of passwords will make that process extremely difficult, especially if the storage of user information and content are divided into separate databases.</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/elements.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/elements.jpg" style="display: inline; "></div>
<p></p>
<h2>Why and How?</h2>
<p>Again this falls under the “non-reliance of the end-user to provide themselves simple security” measure.  Users generally use passwords which are easy to remember, and even use the same passwords across multiple websites(I know, right!?). Easy to remember passwords are generally words found in a dictionary and other kinds of values(ex. 12345, QWERTY).  As developers we often scoff at this practice, but we cannot deny that it’s just the way things are.</p>
<p>In order for a web application to utilize a salt in a password, the application has to store it somewhere.  It’s not recommended to use the same salt across an entire database of passwords, but to generate a unique salt per user.  Generating one salt for an entire database actually decreases the security of the web application in a sense that if an attacker manages to crack it the entire scheme is broke, or if lost, renders the database useless. Creating a full fledged member registration system with all the bells and whistles is out of the scope of this tutorial, however we will be creating a simple system to use an example. Let’s look at generating a salt and applying some randomization techniques:</p>
<p></p>
<h2>1. The Database Connection</h2>
<p>Here is the SQL table that we will be using.</p>
<pre name="code">CREATE TABLE IF NOT EXISTS `users` (
  `usr_id` int(11) NOT NULL AUTO_INCREMENT,
  `usr_name` varchar(24) NOT NULL,
  `usr_pass` varchar(32) NOT NULL,
  `usr_email` varchar(255) NOT NULL,
  `usr_salt` varchar(255) NOT NULL,
  PRIMARY KEY (`usr_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;
</pre>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code213'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123213"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p123code213"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*db_config.php*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//database configuration</span>
<span style="color: #000088;">$db_host</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;localhost&quot;</span> <span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//will likely stay the same</span>
<span style="color: #000088;">$db_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;thedbname&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//the name of the database table</span>
<span style="color: #000088;">$db_usr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//your database username</span>
<span style="color: #000088;">$db_pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//your database password</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Establish a connection with MySQL and select the database to use</span>
<a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_usr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_pass</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQL Error: &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/mysql_select_db"><span style="color: #990000;">mysql_select_db</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_name</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQL Error: &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><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: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h2>2. The Registration File</h2>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code214'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123214"><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
</pre></td><td class="code" id="p123code214"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*registration.php*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//require our db_config.php file</span>
<span style="color: #b1b100;">require</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db_config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Check to see if the form has been submitted</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</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>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Escape our posted inputs</span>
	<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//generate a strong unique salt</span>
	<span style="color: #000088;">$salt_gen</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Combine email, the password and the salt together</span>
	<span style="color: #000088;">$combine</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$email</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$salt_gen</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//md5 hash the combined password * Note: md5 is only used in this scenario as an example</span>
	<span style="color: #000088;">$newpassword</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/md5"><span style="color: #990000;">md5</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combine</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//insert the values into the database</span>
	<span style="color: #000088;">$registerquery</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO users (usr_name, usr_pass, usr_email,  usr_salt) VALUES ('&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$username</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;', '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$newpassword</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;', '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$email</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;', '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$salt_gen</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;')&quot;</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQL Error: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//let the user know of success or failure</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$registerquery</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Success&lt;/h1&gt;'</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: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Failure&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Let’s go over the PHP code.  To keep things simple we include our database config file.  Next PHP checks to see if the form HTML has been submitted by checking if the <code>$_POST</code> variables are not empty.  If they are not empty then the script proceeds to escape the posted form data from the user, preparing it to be inserted into the database.  We then generate a simple salt using <code>uniqid()</code> and <code>mt_rand()</code> and storing it in the variable <code>$salt_gen</code>.  To salt our password we combine the $password, then the salt. Next step, one way hashing the combined variables with md5.</p>
<p>“But wait! You also added the users email to the front of the password and salt combo!” Yup! I did this because, if an attacker gains access to my database in some way, and the salt, the only way the attacker is going to know for sure that the email address is used in the hashing of the password is if they have access to the source code.  How random and unique is an email address?</p>
<p>To top the rest of the PHP code off we insert our variables into the database table within their respective fields, and give the user some feedback on success or failure. Now onto the rest of the registration file, the HTML</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code215'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123215"><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
</pre></td><td class="code" id="p123code215"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&nbsp;
&lt;head&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&nbsp;
&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
	&lt;label for=&quot;username&quot;&gt;Enter a Username&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot; /&gt;&lt;br /&gt;
&nbsp;
    &lt;label for=&quot;email&quot;&gt;Enter your Email&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;email&quot; /&gt;&lt;br /&gt;
&nbsp;
    &lt;label for=&quot;password&quot;&gt;Enter a Password&lt;/label&gt;
    &lt;input type=&quot;password&quot; name=&quot;password&quot; /&gt;&lt;br /&gt;
&nbsp;
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;
&nbsp;
&lt;/body&gt;
&nbsp;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Here we create a simple HTML form which will collect a username, an email, and a password from a user. Nothing fancy here.</p>
<p></p>
<h2>3. Authenticating the User</h2>
<p>So we now have a simple registration form, which inserts a user into the database along with their salted password.  Let’s create a login page which will require us to retrieve information from the database and authenticate the user. First the PHP:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code216'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123216"><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" id="p123code216"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*login.php*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//require our db_config.php file</span>
<span style="color: #b1b100;">require</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db_config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Check to see if the form has been submitted</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</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>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Escape our posted inputs</span>
	<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Grab the row associated with the username from the form</span>
	<span style="color: #000088;">$grab_row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM users WHERE usr_name = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$username</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQL Error: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//If only one row was retrieved</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$grab_row</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//create an array from the row fields</span>
		<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_array"><span style="color: #990000;">mysql_fetch_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$grab_row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//store the users salt in a var</span>
		<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'usr_salt'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//store the users email in a var</span>
		<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'usr_email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//recombine the variables email, password, and the salt</span>
		<span style="color: #000088;">$combine</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$email</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>
&nbsp;
		<span style="color: #666666; font-style: italic;">//re-hash the combined variables Note:md5 is only used in this scenario as an example</span>
		<span style="color: #000088;">$auth_pass</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/md5"><span style="color: #990000;">md5</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combine</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//check the database again for the row associated with the username and the rehashed password</span>
		<span style="color: #000088;">$checklogin</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM users WHERE usr_name = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$username</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' AND usr_pass = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$auth_pass</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQL Error: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//if only one row is retrieved output success or failure to the user</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$checklogin</span><span style="color: #009900;">&#41;</span> <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: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Yippie, we are authenticated!&lt;/h1&gt;'</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: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Oh no, we are not authenticated!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Oh no, we are not in the database!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Basically what we are doing in the login.php file is taking the submitted form variables, grabbing the table row associated with the username and rebuilding the password from the elements in the database it was created with (email, pass, salt) and rehashing them.  We then check the database again for the username AND the rehashed password value to find a match, outputting the user on success or failure. Finally here is the HTML:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code217'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123217"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p123code217"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
	&lt;label for=&quot;username&quot;&gt;Enter your Username&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot; /&gt;&lt;br /&gt;
&nbsp;
    &lt;label for=&quot;password&quot;&gt;Enter a Password&lt;label&gt;
    &lt;input type=&quot;password&quot; name=&quot;password&quot; /&gt;&lt;br /&gt;
&nbsp;
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p></p>
<h2>Obfuscation in PHP</h2>
<p>A simple yet complex definition of obfuscation is (use the version contained in the source if you wish to run the code):</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code218'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123218"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p123code218"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$a1c0_z2</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'c'</span><span style="color: #339933;">.</span><span style="color: #000088;">$a91</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'tion '</span><span style="color: #339933;">;</span><span style="color: #000088;">$a91</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$vly_ti</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;us&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'ed'</span><span style="color: #339933;">;</span><span style="color: #000088;">$j1h_32_a</span><span style="color: #339933;">=</span><span style="color: #0000ff;">' to'</span><span style="color: #339933;">;</span><span style="color: #000088;">$z1b_1</span><span style="color: #339933;">=</span><span style="color: #000088;">$a91</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$lz32i_4</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;“O&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;bfus&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$g1k0p</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'que '</span><span style="color: #339933;">;</span><span style="color: #000088;">$lv83</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;t&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'ec'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'hni'</span><span style="color: #339933;">;</span><span style="color: #000088;">$lFa</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'i'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'s '</span><span style="color: #339933;">;</span>if<span style="color: #009900;">&#40;</span><span style="color: #000088;">$z1b_1</span><span style="color: #339933;">==</span><span style="color: #000088;">$a91</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #000088;">$rx_b_1</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span><span style="color: #000088;">$glccUv</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; complic&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$rx_b_1</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'te '</span><span style="color: #339933;">;</span><span style="color: #000088;">$xl1ttf</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'code '</span><span style="color: #339933;">;</span><span style="color: #000088;">$zljal1</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;in such a&quot;</span><span style="color: #339933;">;</span>if<span style="color: #009900;">&#40;</span><span style="color: #000088;">$z1b_1</span><span style="color: #339933;">==</span><span style="color: #000088;">$a91</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #000088;">$s1b_1</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span><span style="color: #000088;">$p1x2</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; w&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$s1b_1</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;y &quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$il_7x</span><span style="color: #339933;">=</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #000088;">$b1zE_</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'t i'</span><span style="color: #339933;">.</span><span style="color: #000088;">$l1yes</span><span style="color: #339933;">;</span><span style="color: #000088;">$b1zE_</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;i&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$l1yes</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;s&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$nltotry_ws</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'st'</span><span style="color: #339933;">.</span><span style="color: #000088;">$s1b_1</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$yl5B_</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'thαt '</span><span style="color: #339933;">;</span><span style="color: #000088;">$dlno</span><span style="color: #339933;">=</span><span style="color: #0000ff;">' not '</span><span style="color: #339933;">;</span><span style="color: #000088;">$m1tomanythings</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;under&quot;</span><span style="color: #339933;">;</span>if<span style="color: #009900;">&#40;</span><span style="color: #000088;">$s1b_1</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #000088;">$bz_1</span><span style="color: #339933;">=</span><span style="color: #000088;">$s1b_1</span><span style="color: #339933;">;</span><span style="color: #000088;">$Ozaq</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;d&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$bz_1</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;ble&quot;</span><span style="color: #0000ff;">&quot;;echo base64_decode(&quot;</span>JiM4MjIwO09iZnVzY3Rpb24mIzgyMDE7aXMmIzgyMDE7YSYjODIwMTt0ZWNobmlxdWUmIzgyMDE7dXNlZCYjODIwMTt0byYjODIwMWNvbXBsaWNhdGUmIzgyMDE7Y29kZSYjODIwMTtpbiYjODIwMTtzdWNoJiM4MjAxO2EmIzgyMDE7d2F5JiM4MjAxO3RoJmFscGhhO3QmIzgyMDE7aSYjODIwMTt0JiM4MjAxO2kmIzgyMDE7bm90JiM4MjAxO3VuZGVyc3RhbmRhYmxlJnF1b3Q7<span style="color: #0000ff;">&quot;);?&gt;</span></pre></td></tr></table></div>

<p>As you can see, this code is not meant to be distinguishable. There are no distinct variable names, there are no comments, no spacing, no indentation, no distinct order and it’s all in one line.  Even though we cannot distinquish the code, our machines still know what it is. It works.  This one line of chaos simply echos “Obfusction is a technique used to complicate code in such a way that i t i not understandable.”  Yes, I know about the errors.</p>
<p>Obfuscation has pros and cons.  It’s purpose is to disuade a person from finding out what code is doing at a glance, or for a period of time. This is a plus toward individuals with little to no knowledge of the programming language.  However, anybody who has a basic understanding of PHP can disseminate the above obfuscated code and figure out what it’s doing, it might just take a little time.  This is one of the flaws of obfuscation, it is not a form of encryption, it’s just an attempt to be cryptic.  Obfuscation also normally adds to filesize. A lot of the time, you’ll encounter obfuscated code in propriatary and malicious software.</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/obfuscation.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/obfuscation.jpg" style="display: inline; "></div>
<p></p>
<h2>So How Can I Obfuscate My Code?</h2>
<p>This is a common question.  There are primarily two ways to obfuscate your code.  First, you can do it by hand.  Writing obfuscated code takes a long time.  The example used in this article took a while to write because of the same reasons you use obfuscation in the first place (lack of structure, order etc…), this even resulted in some menial errors which I didn’t even want to hunt down and fix.  The second way you can obfuscate your code is by buying software that does it for you.  Using a program to obfuscate code is trivial, and of course costs money a lot of the time.  Some software which claims to obfuscate your code, actually encrypts and/or encodes it in such a way it relies on a handshake to function. Often you’ll find software whose vendor won’t even guarantee your code will work when it’s done.  Even in the example, I used a simple <code>Base64 </code>function to encode the construction of the script output.</p>
<p></p>
<h2>Some Obfuscation Tips</h2>
<ul>
<li>Always, always, keep a clean version of the source for yourself.</li>
<li>The more random your technique, the better.</li>
<li>Eliminate all whitespace, where it is not needed.</li>
<li>Character Encode printed/echo’ed characters and spaces (i.e. quotations, thin spaces, apostropes, hypens)
</li>
<li>The more complex the code, the better.</li>
<li>Disregard structure unless it is detrimental to the operation of the code(e.x. variable locations before they’re called)</li>
<li>Do not use distinguishable variable names, namespaces, or class names.</li>
<li>The less code you reuse, the better</li>
<li>Don’t believe it’s foolproof</li>
</ul>
<p></p>
<h2>To Obfuscate or Not to Obfuscate?</h2>
<p>It really depends on your plan. Particularly if your looking to sell your PHP script (or any software) you need to license it.  This is going to be one of the front line defenses to thwart the softwares intended audience from doing whatever they want. However, you may want to obfuscate some, or all of your code for whatever reason. However due to obfuscations negatives, if your really that worried about the security of your source code, it may be worth  looking to encryption instead.</p>
<p></p>
<h2>Cryptography in PHP</h2>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/quoteimage.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/quoteimage.jpg" style="display: inline; "></div>
<p>Wikipedia.com defines cryptography as:</p>
<p style="text-align:center; font-style:italic;">“the practice and study of hiding information.”</p>
<p>Cryptography is a big deal, wether your aware of it or not.  In almost every web application presently deployed there is some presence of cryptography being utilized (i.e. mail clients and websites). As developers we need to be informed and aware of the practical applications of cryptography within our software.  PHP provides us with some very fundamental and practical functions we can utilize to encrypt data.  In this section, I will be mainly going over one-way hashing algorithms though I will touch lightly on Symmetric-key based encryption. There are plenty more (i.e. Steganography, Asymmetric-Key to name a couple).</p>
<p></p>
<h2>The One Way Hash</h2>
<p>Alot of the time we utilize one-way hashing as a way to securely store passwords and check the data integrity of files.  While we do this, to authenticate members of a web application we hash the users entered password, and match it against the users stored hash.  The same technique applies to checking the integrity of files.</p>
<p style="font-weight:bold;">SHA-1, 2, and 3</p>
<p>The SHA family of hash algorithms are currently the most popular, significantly SHA-1.  Even though the SHA-1 algorithm may have a weakness, it is still in wide use.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code219'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123219"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p123code219"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">///One way hashing with SHA-1</span>
&nbsp;
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GigaSpartan is Awesome&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sha1"><span style="color: #990000;">sha1</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//or</span>
<span style="color: #000088;">$hash2</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/hash"><span style="color: #990000;">hash</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha1'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash2</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Will output: 42d2f15c3f92d28d7d58776e5d81b800f662cc6c</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In PHP, SHA-2 is called upon in a different respect, and requires PHP 5 greater than or equal to 5.1.2.  SHA-2 is superior to SHA-1 and can be called with different bit sizes.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code220'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123220"><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
</pre></td><td class="code" id="p123code220"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$string_sha256</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GigaSpartan is Awesome&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string_sha384</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GigaSpartan is Awesome&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string_sha512</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GigaSpartan is Awesome&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$hash_sha256</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/hash"><span style="color: #990000;">hash</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha256'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string_sha256</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$hash_sha384</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/hash"><span style="color: #990000;">hash</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha384'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string_sha384</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$hash_sha512</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/hash"><span style="color: #990000;">hash</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha512'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string_sha512</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash_sha256</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash_sha384</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash_sha512</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs repspectively:
sha256 : 09074adc0d70e15b88494643e29c2836e1ab94a21989691dec594cb0bd742ebc
sha384 : 8535470750df54a78701d4bfe0451f9799057a5bc101944a32480d2436e8b95440bce3bcab3f9ce107b0b92d9595ae32
sha512 : c2e6dce873a71800b862791e56b480b976bb26cd3136c02da510c3905caa49b7b9e9260549976e1e741cc93e4569a611f2030d3b7104c6c6c2ff9e6c9bf0946a
*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The hash function is called by hash(algorithm, string); In the newest PHP versions the hash() function can be used to call any one-way hash algorithm PHP supports (i.e. md5, sha-1, haval, ghost). If you want to see a list of all the registered hashing algorithms you can use:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code221'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123221"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p123code221"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//As of PHP5 &gt;= 5.1.2</span>
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/hash_algos"><span style="color: #990000;">hash_algos</span></a><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: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>SHA-3 is still being developed and considered for standardization.  A <a href="http://www.csrc.nist.gov/groups/ST/hash/index.html" title="NIST">competition</a> to find a good candidate to act as the new secure hash algorithm was launched by the National Institute of Standards and Technology and entries for the competition were deadlined for October 31, 2008.  A rather popular entry named Skein,  has an available PHP module you can download (though you have to compile it yourself).  Skein is developed by some big names found within the security industry such as Bruce Schneier, Niels Ferguson, and Stefan Lucks to name a few.  The official Skein website can be found <a href="http://www.skein-hash.info" title="Skein Official Site">here</a>.</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/encrypt.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/encrypt.jpg" style="display: inline; "></div>
<p></p>
<h2>Key-based Encryption</h2>
<p>Symmetric-Key encryption methods is where the security of the encryption primarily resides within a key, which is shared between two points, where the data is encrypted and where the data is decrypted.  A very good example of how this can work was provided by my “<a href="http://gigaspartan.com/2010/12/29/tutorial-creating-a-crypter-class-with-php/" title="Creating a Crypter Class with PHP">Creating a Crypter Class with PHP</a>” tutorial.</p>
<p></p>
<h2>HMAC</h2>
<p>Essentially HMAC is like a mix between one-way hashing and key based encryption.  HMAC security relies on the key size used, and strength of the hash function it is calculated with.  You can somewhat compare this method to salting passwords.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code222'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123222"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p123code222"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$string_hmac</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GigaSpartan is Awesome&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//hash_hmac(algorithm, string to hash, key)</span>
<span style="color: #000088;">$hmac</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/hash_hmac"><span style="color: #990000;">hash_hmac</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha1'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string_hmac</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'secret'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hmac</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h2>Wrapping it All Up</h2>
<p>Well what a journey!  Randomizing values, generating random passwords, salting, storing and authenticating users, obfuscation, crypto…seems like alot to take in.  But worth it!  It’s important to know what kind of security your going to implement into your web applications, and how your going to protect it.  Even more, it’s important to keep a clever attitude towards these implementations and not think that security is only implemented by a few methods, but by a combination of them, with a dash of creativity.</p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2010/12/31/tutorial-simple-techniques-to-lock-down-your-website/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutorial: Creating a Crypter Class with PHP</title>
		<link>http://gigaspartan.com/2010/12/29/tutorial-creating-a-crypter-class-with-php/</link>
		<comments>http://gigaspartan.com/2010/12/29/tutorial-creating-a-crypter-class-with-php/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 04:02:00 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[alogorithms]]></category>
		<category><![CDATA[decrypt]]></category>
		<category><![CDATA[encrypt]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=114</guid>
		<description><![CDATA[In this article I will explain how to create a PHP Class that will encrypt and decrypt any data with a given password. It is object programmed and uses existing PHP algorithms. Introduction Think about what we might need a class like this for? We want to encrypt important data with a password for security [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I will explain how to create a PHP Class that will encrypt and decrypt any data with a given password. It is object programmed and uses existing PHP algorithms.
</p>
<p><span id="more-6941"></span></p>
<p></p>
<h2>Introduction</h2>
<p>Think about what we might need a class like this for? We want to encrypt important data with a password for security reasons. We also want, as already mentioned, to be able to decrypt that data when necessary. Why should you use symmetric algorithms? It’s easy; when you’re offering a password sent via email or something like that, you need the password to be sent in plaintext. The hash algorithms are not reversible. Once you have hashed a string you can’t decipher the original text from the hash. </p>
<p>Maybe you have already heard of MD5? It’s not really the best option anymore because it tends to be unsafe. There are databases around the web – that I don’t want to mention – that can be used to retrieve the plaintext from a hash simply by typing in the hash into a search box. So you should use something like SHA which was developed by the NSA (National Security Agency). SHA is the abbreviation for Secure Hash Algorithm and is one of the most secure hash algorithms. There are some others as well, such as WHIRLPOOL, PANAMA and RIPEMD, but SHA is currently the secure standard for hashes and is used in numerous applications.
</p>
<p><span id="more-114"></span><br />
</p>
<h2><span>Step 1:</span>  Preparation</h2>
<p>I think it is important to create an interface. This is because we can always use the methods which are defined in the interface without thinking, when instancing an object of a class, which implements that interface. </p>
<p>
	When a class implements an interface it has to implement the methods given in that interface, otherwise there will be an error! So here is an example: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code240'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114240"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p114code240"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">interface</span> ICrypter<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">class</span> Crypter implements ICrypter<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>
	As you can see, the interface instructs the classes which implement<code> ICrypter</code> to have the public function Encrypt with one parameter <code>$data</code>. The public function Decrypt also has the parameter $<code>data</code>. You can try it out; if the class lacks one of the given methods in the interface, you get a fatal error. Here’s an example:</p>
<blockquote>
<p>
	Fatal error: Class Crypter contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ICrypter::Decrypt) in C:\www\Giga\Crypter\crypter.php on line 32. </p>
</blockquote>
<p>	Nice error right? So you can be sure that the classes really have the methods!
</p>
<div class="tutorial_image">
	<img border="0" src="http://farm4.static.flickr.com/3317/3619465338_e0b46b5d23_o.jpg" original="http://farm4.static.flickr.com/3317/3619465338_e0b46b5d23_o.jpg" style="display: inline; ">
</div>
<p></p>
<h2><span>Step 2:</span> Password for Encryption and Decryption</h2>
<p>
	As I said before, we want to be able to use a specific password for encryption and decryption. This password has to be accessible for the encrypt- and decrypt-function so we will define an instance variable, called key, which is passed to the constructor. The definition of <code>$Key</code> is only needed in the C<code>rypter</code> Class: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code241'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114241"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code241"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$Key</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	However, the definition of the constructor has to be in the interface. Therefore, it is also needed in the class, because we have to implement everything we have defined in the interface. The interface will contain: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code242'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114242"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code242"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>	and the class: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code243'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114243"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code243"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</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></td></tr></table></div>

<p>Now that we know we get a key, we can use it to encrypt and decrypt!
</p>
<div class="tutorial_image">
	<img border="0" src="http://farm4.static.flickr.com/3595/3619465342_5474e711d2_o.jpg" original="http://farm4.static.flickr.com/3595/3619465342_5474e711d2_o.jpg" style="display: inline; ">
</div>
<p></p>
<h2><span>Step 3:</span> Constructor</h2>
<p>
	In the constructor we have to set the key and choose an algorithm. We will use the Blowfish algorithm for this example and use it as a standard value. I will explain a bit more about the symmetric algorithms later in the text, but for simplicity we will use Blowfish. You can change this later if you want to. So we need another instance variable called Algo: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code244'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114244"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code244"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$Algo</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>	and the constructor… </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code245'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114245"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p114code245"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Algo</span> <span style="color: #339933;">=</span> MCRYPT_BLOWFISH<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Key <span style="color: #339933;">=</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <a href="http://www.php.net/mcrypt_get_key_size"><span style="color: #990000;">mcrypt_get_key_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Algo</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Algo <span style="color: #339933;">=</span> <span style="color: #000088;">$Algo</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>
	The length of the key depends on the algorithm and the encryption mode. In this example we will use the ECB mode. You can make this variable like we have already done with the algorithm. We use the substring of the given key with the maximum allowed length. You can get this length with the<code> mcrypt_get_key_size </code>function which requires the algorithm and the encryption mode as parameters. </p>
<p>
	Now we give our instance variable Key the correct key for the algorithm and assign our instance variable Algo. </p>
<p>
	So now we have the constructor. As I said previously, you can change the standard value of Algo to any other algorithm that is supported by MCrypt. </p>
<p>
	List of supported algorithms from php.net: </p>
<ul>
<li>MCRYPT_3DES</li>
<li>MCRYPT_ARCFOUR_IV (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_ARCFOUR (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_BLOWFISH</li>
<li>MCRYPT_CAST_128</li>
<li>MCRYPT_CAST_256</li>
<li>MCRYPT_CRYPT</li>
<li>MCRYPT_DES</li>
<li>MCRYPT_DES_COMPAT (libmcrypt 2.2.x only)</li>
<li>MCRYPT_ENIGMA (libmcrypt &gt; 2.4.x only, alias for MCRYPT_CRYPT)</li>
<li>MCRYPT_GOST</li>
<li>MCRYPT_IDEA (non-free)</li>
<li>MCRYPT_LOKI97 (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_MARS (libmcrypt &gt; 2.4.x only, non-free)</li>
<li>MCRYPT_PANAMA (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_RIJNDAEL_128 (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_RIJNDAEL_192 (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_RIJNDAEL_256 (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_RC2</li>
<li>MCRYPT_RC4 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_RC6 (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_RC6_128 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_RC6_192 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_RC6_256 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_SAFER64</li>
<li>MCRYPT_SAFER128</li>
<li>MCRYPT_SAFERPLUS (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_SERPENT(libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_SERPENT_128 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_SERPENT_192 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_SERPENT_256 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_SKIPJACK (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_TEAN (libmcrypt 2.2.x only)</li>
<li>MCRYPT_THREEWAY</li>
<li>MCRYPT_TRIPLEDES (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_TWOFISH (for older mcrypt 2.x versions, or mcrypt &gt; 2.4.x )</li>
<li>MCRYPT_TWOFISH128 (TWOFISHxxx are available in newer 2.x versions, but not in the 2.4.x versions)</li>
<li>MCRYPT_TWOFISH192</li>
<li>MCRYPT_TWOFISH256</li>
<li>MCRYPT_WAKE (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_XTEA (libmcrypt &gt; 2.4.x only)</li>
</ul>
<p>
  	So which one should we use when we want to use the Crypter Class in our products? At the moment AES is the standard of the symmetric algorithms. It is used in many	applications, but where is AES? AES was originally published as Rijndael which is listed. It is a really fast, but secure, algorithm and is even fast with 256-Bit key size. My advice is to use MCRYPT_RIJNDAEL_256 for your applications. Just as an example,	AES is used in WPA2 which is a security standard for WLAN.
</p>
<div class="tutorial_image">
	<img border="0" src="http://farm4.static.flickr.com/3655/3619465344_c358737684_o.jpg" original="http://farm4.static.flickr.com/3655/3619465344_c358737684_o.jpg" style="display: inline; ">
</div>
<p></p>
<h2><span>Step 4:</span> Now to the Encryption</h2>
<p>
	First thing to check: is there any data to encrypt? If not, you can go ahead and break the encryption. If you want to use any other encryption modes then you have to add the following code. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code246'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114246"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p114code246"><pre class="php" style="font-family:monospace;">		<span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_get_iv_size"><span style="color: #990000;">mcrypt_get_iv_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Algo<span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_create_iv"><span style="color: #990000;">mcrypt_create_iv</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	This <code>$iv</code> is used for example in CBC, CFB, OFB and in some algorithms in STREAM encryption mode. If the parameter is not passed in these modes, the <code>$iv</code> will be set to <code>'\0'</code>. The next step is to encrypt the data with the simple function mcrypt_encrypt. Here we need our algorithm, the key, the data and an encryption mode. $iv is optional. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code247'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114247"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code247"><pre class="php" style="font-family:monospace;">		<span style="color: #000088;">$crypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_encrypt"><span style="color: #990000;">mcrypt_encrypt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Algo<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Key<span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #339933;">,</span> <span style="color: #000088;">$iv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	Finally encode the encrypted data with base64_encode and trim it before you return it. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code248'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114248"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code248"><pre class="php" style="font-family:monospace;">		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/base64_encode"><span style="color: #990000;">base64_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crypt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	We have to base64 encode the encrypted data to get URL-Safe data. This is needed because, if you want to use the encrypted data, for example in a URL, you will have problems with ‘&amp;’ as it is a reserved character specified in the RFC. So you need something like alphanumeric characters – in other words, character that are safe. The base64 encode supplies these safe characters, which is why we’re using it. We do not know what will be done with the data after encryption. </p>
</p>
<div class="tutorial_image">
	<img border="0" src="http://farm3.static.flickr.com/2470/3619465346_cdf1928555_o.jpg" original="http://farm3.static.flickr.com/2470/3619465346_cdf1928555_o.jpg" style="display: inline; ">
</div>
<p></p>
<h2><span>Step 5:</span> Decryption is Reversed Encryption</h2>
<p>
	Again we ask the same first question. Is there data? If there is, you have to base64_decode the data as we have previously encoded it with base64_encode.  </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code249'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114249"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code249"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$crypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/base64_decode"><span style="color: #990000;">base64_decode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	Then the optional part with $iv. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code250'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114250"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p114code250"><pre class="php" style="font-family:monospace;">			<span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_get_iv_size"><span style="color: #990000;">mcrypt_get_iv_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Algo<span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_create_iv"><span style="color: #990000;">mcrypt_create_iv</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	Decryption with the simple function <code>mcrypt_decrypt</code>. Here we need – nearly – the same parameters. The difference is that the decrypt-function needs to access the crypted data rather than the original data. So here again we use, the algorithm, the key, the crypted data, the encryption mode, and the optional iv. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code251'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114251"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code251"><pre class="php" style="font-family:monospace;">		<span style="color: #000088;">$decrypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_decrypt"><span style="color: #990000;">mcrypt_decrypt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Algo<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Key<span style="color: #339933;">,</span> <span style="color: #000088;">$crypt</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #339933;">,</span> <span style="color: #000088;">$iv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>	Finally return the trimmed and decrypted data. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code252'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114252"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code252"><pre class="php" style="font-family:monospace;">		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decrypt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<div class="tutorial_image">
	<img border="0" src="http://farm4.static.flickr.com/3339/3619465348_25e097ecf2_o.jpg" original="http://farm4.static.flickr.com/3339/3619465348_25e097ecf2_o.jpg" style="display: inline; ">
</div>
<p></p>
<h2>Examples</h2>
<p>Define a global Crypter. In this example we will use RIJNDAEL_256 (AES) with the password “Any password”. After instancing you call your functions or methods to test it. Here we call the function <code>foo</code> and the method <code>foo1</code>. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code253'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114253"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p114code253"><pre class="php" style="font-family:monospace;">		<span style="color: #000088;">$crypter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Crypter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Any password&quot;</span><span style="color: #339933;">,</span> MCRYPT_RIJNDAEL_256<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$foo</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>foo1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	You can get your crypter from the Superglobal variable called <code>$GLOBALS</code>. This is an associative array, so you can call all your global variables by the name you defined them with. You can retrieve the <code>$crypter</code> which is defined outside	of the foo or foo1 block with <code>$GLOBALS["crypter"]</code>… </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code254'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114254"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p114code254"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #339933;">...</span>
			<span style="color: #000088;">$encrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$decrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$encrypted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #339933;">...</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">class</span> Foo<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> foo1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #339933;">...</span>
				<span style="color: #000088;">$encrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$decrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$encrypted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #339933;">...</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></p>
<h2>Conclusion</h2>
<p>Here is the final class:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code255'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114255"><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" id="p114code255"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> ICrypter<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Algo</span> <span style="color: #339933;">=</span> MCRYPT_BLOWFISH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Crypter implements ICrypter<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$Key</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$Algo</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Algo</span> <span style="color: #339933;">=</span> MCRYPT_BLOWFISH<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Key</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <a href="http://www.php.net/mcrypt_get_key_size"><span style="color: #990000;">mcrypt_get_key_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Algo</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Algo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$Algo</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> Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Optional Part, only necessary if you use other encryption mode than ECB</span>
		<span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_get_iv_size"><span style="color: #990000;">mcrypt_get_iv_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Algo</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_create_iv"><span style="color: #990000;">mcrypt_create_iv</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$crypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_encrypt"><span style="color: #990000;">mcrypt_encrypt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Algo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #339933;">,</span> <span style="color: #000088;">$iv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/base64_encode"><span style="color: #990000;">base64_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crypt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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> Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$crypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/base64_decode"><span style="color: #990000;">base64_decode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Optional Part, only necessary if you use other encryption mode than ECB</span>
		<span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_get_iv_size"><span style="color: #990000;">mcrypt_get_iv_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Algo</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_create_iv"><span style="color: #990000;">mcrypt_create_iv</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$decrypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_decrypt"><span style="color: #990000;">mcrypt_decrypt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Algo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$crypt</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #339933;">,</span> <span style="color: #000088;">$iv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decrypt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</p>
<p>And here is another example:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code256'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114256"><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
</pre></td><td class="code" id="p114code256"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'crypter.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Creating a new instance of the crypter with RIJNDAEL_256 encryption</span>
<span style="color: #000088;">$crypter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Crypter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Any password&quot;</span><span style="color: #339933;">,</span> MCRYPT_RIJNDAEL_256<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Procedual</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Procedual&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Object oriented</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Object oriented&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo1</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;">//Procedual - Function</span>
<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Teststring&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$encrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Encrypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$decrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Decrypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$encrypted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Original: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$data</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Encrypted: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$encrypted</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Decrypted: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$decrypted</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Object oriented - Method</span>
<span style="color: #000000; font-weight: bold;">class</span> Foo<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> foo1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Teststring&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$encrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Encrypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$decrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Decrypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$encrypted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Original: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$data</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Encrypted: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$encrypted</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Decrypted: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$decrypted</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</p>
<p>
	Now you have a complete Crypter class and you can crypt and decrypt as many times as you wish! I hope that you have enjoyed this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2010/12/29/tutorial-creating-a-crypter-class-with-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial: 9 Useful PHP Functions and Features You Need to Know</title>
		<link>http://gigaspartan.com/2010/12/25/tutorial-9-useful-php-functions-and-features-you-need-to-know/</link>
		<comments>http://gigaspartan.com/2010/12/25/tutorial-9-useful-php-functions-and-features-you-need-to-know/#comments</comments>
		<pubDate>Sun, 26 Dec 2010 01:03:55 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=98</guid>
		<description><![CDATA[Even after using PHP for years, we stumble upon functions and features that we did not know about. Some of these can be very useful, yet underused. Not all of us have read the manual and the function reference from cover to cover! 1. Functions with Arbitrary Number of Arguments You may already know that [...]]]></description>
			<content:encoded><![CDATA[<p>Even after using PHP for years, we stumble upon functions and features that we did not know about. Some of these can be very useful, yet underused. Not all of us have read the manual and the function reference from cover to cover!</p>
<p></p>
<h2><span>1. </span>Functions with Arbitrary Number of Arguments</h2>
<p>You may already know that PHP allows you to define functions with optional arguments. But there is also a method for allowing completely arbitrary number of function arguments.</p>
<p>First, here is an example with just optional arguments:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code278'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98278"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p98code278"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// function with 2 optional arguments</span>
<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arg1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arg2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;arg1: <span style="color: #006699; font-weight: bold;">$arg1</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;arg2: <span style="color: #006699; font-weight: bold;">$arg2</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
foo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hello'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'world'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints:
arg1: hello
arg2: world
*/</span>
&nbsp;
foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints:
arg1:
arg2:
*/</span></pre></td></tr></table></div>

<p>Now, let’s see how we can build a function that accepts any number of arguments. This time we are going to utilize <a href="http://us2.php.net/manual/en/function.func-get-args.php">func_get_args()</a>:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code279'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98279"><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
</pre></td><td class="code" id="p98code279"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// yes, the argument list can be empty</span>
<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// returns an array of all passed arguments</span>
	<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/func_get_args"><span style="color: #990000;">func_get_args</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;arg&quot;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</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: #0000ff;">&quot;: <span style="color: #006699; font-weight: bold;">$v</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints nothing */</span>
&nbsp;
foo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hello'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
arg1: hello
*/</span>
&nbsp;
foo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hello'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'world'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'again'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
arg1: hello
arg2: world
arg3: again
*/</span></pre></td></tr></table></div>

<p><span id="more-98"></span><br />
</p>
<h2><span>2. </span>Using Glob() to Find Files</h2>
<p>Many PHP functions have long and descriptive names. However it may be hard to tell what a function named <a href="http://us.php.net/manual/en/function.glob.php">glob()</a> does unless you are already familiar with that term from elsewhere.</p>
<p>Think of it like a more capable version of the <a href="http://php.net/manual/en/function.scandir.php">scandir()</a> function. It can let you search for files by using patterns.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code280'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98280"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p98code280"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// get all php files</span>
<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/glob"><span style="color: #990000;">glob</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'*.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* output looks like:
Array
(
    [0] =&amp;gt; phptest.php
    [1] =&amp;gt; pi.php
    [2] =&amp;gt; post_output.php
    [3] =&amp;gt; test.php
)
*/</span></pre></td></tr></table></div>

<p>You can fetch multiple file types like this:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code281'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98281"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p98code281"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// get all php files AND txt files</span>
<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/glob"><span style="color: #990000;">glob</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'*.{php,txt}'</span><span style="color: #339933;">,</span> GLOB_BRACE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* output looks like:
Array
(
    [0] =&amp;gt; phptest.php
    [1] =&amp;gt; pi.php
    [2] =&amp;gt; post_output.php
    [3] =&amp;gt; test.php
    [4] =&amp;gt; log.txt
    [5] =&amp;gt; test.txt
)
*/</span></pre></td></tr></table></div>

<p>Note that the files can actually be returned with a path, depending on your query:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code282'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98282"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p98code282"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/glob"><span style="color: #990000;">glob</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'../images/a*.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* output looks like:
Array
(
    [0] =&amp;gt; ../images/apple.jpg
    [1] =&amp;gt; ../images/art.jpg
)
*/</span></pre></td></tr></table></div>

<p>If you want to get the full path to each file, you can just call the <a href="http://php.net/manual/en/function.realpath.php">realpath()</a> function on the returned values:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code283'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98283"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p98code283"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/glob"><span style="color: #990000;">glob</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'../images/a*.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// applies the function to each array element</span>
<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_map"><span style="color: #990000;">array_map</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'realpath'</span><span style="color: #339933;">,</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* output looks like:
Array
(
    [0] =&amp;gt; C:\wamp\www\images\apple.jpg
    [1] =&amp;gt; C:\wamp\www\images\art.jpg
)
*/</span></pre></td></tr></table></div>

<p></p>
<h2><span>3. </span>Memory Usage Information</h2>
<p>By observing the memory usage of your scripts, you may be able optimize your code better.</p>
<p>PHP has a garbage collector and a pretty complex memory manager. The amount of memory being used by your script. can go up and down during the execution of a script. To get the current memory usage, we can use the <a href="http://us2.php.net/manual/en/function.memory-get-usage.php">memory_get_usage()</a> function, and to get the highest amount of memory used at any point, we can use the <a href="http://us2.php.net/manual/en/function.memory-get-peak-usage.php">memory_get_peak_usage()</a> function.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code284'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98284"><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
</pre></td><td class="code" id="p98code284"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Initial: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/memory_get_usage"><span style="color: #990000;">memory_get_usage</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; bytes <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
Initial: 361400 bytes
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// let's use up some memory</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: #cc66cc;">100000</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;">$array</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span> <a href="http://www.php.net/md5"><span style="color: #990000;">md5</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// let's remove half of the array</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: #cc66cc;">100000</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>
	<a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</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: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Final: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/memory_get_usage"><span style="color: #990000;">memory_get_usage</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; bytes <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
Final: 885912 bytes
*/</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Peak: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/memory_get_peak_usage"><span style="color: #990000;">memory_get_peak_usage</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; bytes <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
Peak: 13687072 bytes
*/</span></pre></td></tr></table></div>

<p></p>
<h2><span>4. </span>CPU Usage Information</h2>
<p>For this, we are going to utilize the <a href="http://us2.php.net/manual/en/function.getrusage.php">getrusage()</a> function. Keep in mind that this is not available on Windows platforms.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code285'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98285"><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
</pre></td><td class="code" id="p98code285"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/getrusage"><span style="color: #990000;">getrusage</span></a><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: #666666; font-style: italic;">/* prints
Array
(
    [ru_oublock] =&amp;gt; 0
    [ru_inblock] =&amp;gt; 0
    [ru_msgsnd] =&amp;gt; 2
    [ru_msgrcv] =&amp;gt; 3
    [ru_maxrss] =&amp;gt; 12692
    [ru_ixrss] =&amp;gt; 764
    [ru_idrss] =&amp;gt; 3864
    [ru_minflt] =&amp;gt; 94
    [ru_majflt] =&amp;gt; 0
    [ru_nsignals] =&amp;gt; 1
    [ru_nvcsw] =&amp;gt; 67
    [ru_nivcsw] =&amp;gt; 4
    [ru_nswap] =&amp;gt; 0
    [ru_utime.tv_usec] =&amp;gt; 0
    [ru_utime.tv_sec] =&amp;gt; 0
    [ru_stime.tv_usec] =&amp;gt; 6269
    [ru_stime.tv_sec] =&amp;gt; 0
)
&nbsp;
*/</span></pre></td></tr></table></div>

<p>That may look a bit cryptic unless you already have a system administration background. Here is the explanation of each value (you don&#8217;t need to memorize these):</p>
<ul>
<li>ru_oublock: block output operations</li>
<li>ru_inblock: block input operations</li>
<li>ru_msgsnd: messages sent</li>
<li>ru_msgrcv: messages received</li>
<li>ru_maxrss: maximum resident set size</li>
<li>ru_ixrss: integral shared memory size</li>
<li>ru_idrss: integral unshared data size</li>
<li>ru_minflt: page reclaims</li>
<li>ru_majflt: page faults</li>
<li>ru_nsignals: signals received</li>
<li>ru_nvcsw: voluntary context switches</li>
<li>ru_nivcsw: involuntary context switches</li>
<li>ru_nswap: swaps</li>
<li>ru_utime.tv_usec: user time used (microseconds)</li>
<li>ru_utime.tv_sec: user time used (seconds)</li>
<li>ru_stime.tv_usec: system time used (microseconds)</li>
<li>ru_stime.tv_sec: system time used (seconds)</li>
</ul>
<p>To see how much CPU power the script has consumed, we need to look at the &#8216;user time&#8217; and &#8216;system time&#8217; values. The seconds and microseconds portions are provided separately by default. You can divide the microseconds value by 1 million, and add it to the seconds value, to get the total seconds as a decimal number.</p>
<p>Let&#8217;s see an example:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code286'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98286"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p98code286"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// sleep for 3 seconds (non-busy)</span>
<a href="http://www.php.net/sleep"><span style="color: #990000;">sleep</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/getrusage"><span style="color: #990000;">getrusage</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;User time: &quot;</span><span style="color: #339933;">.</span>
	<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_utime.tv_sec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span>
	<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_utime.tv_usec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;System time: &quot;</span><span style="color: #339933;">.</span>
	<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_stime.tv_sec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span>
	<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_stime.tv_usec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* prints
User time: 0.011552
System time: 0
*/</span></pre></td></tr></table></div>

<p>Even though the script took about 3 seconds to run, the CPU usage was very very low. Because during the sleep operation, the script actually does not consume CPU resources. There are many other tasks that may take real time, but may not use CPU time, like waiting for disk operations. So as you see, the CPU usage and the actual length of the runtime are not always the same.</p>
<p>Here is another example:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code287'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98287"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p98code287"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// loop 10 million times (busy)</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: #cc66cc;">10000000</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>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/getrusage"><span style="color: #990000;">getrusage</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;User time: &quot;</span><span style="color: #339933;">.</span>
	<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_utime.tv_sec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span>
	<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_utime.tv_usec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;System time: &quot;</span><span style="color: #339933;">.</span>
	<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_stime.tv_sec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span>
	<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_stime.tv_usec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* prints
User time: 1.424592
System time: 0.004204
*/</span></pre></td></tr></table></div>

<p>That took about 1.4 seconds of CPU time, almost all of which was user time, since there were no system calls.</p>
<p>System Time is the amount of time the CPU spends performing system calls for the kernel on the program&#8217;s behalf. Here is an example of that:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code288'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98288"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p98code288"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// keep calling microtime for about 3 seconds</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/getrusage"><span style="color: #990000;">getrusage</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;User time: &quot;</span><span style="color: #339933;">.</span>
	<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_utime.tv_sec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span>
	<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_utime.tv_usec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;System time: &quot;</span><span style="color: #339933;">.</span>
	<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_stime.tv_sec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span>
	<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ru_stime.tv_usec'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* prints
User time: 1.088171
System time: 1.675315
*/</span></pre></td></tr></table></div>

<p>Now we have quite a bit of system time usage. This is because the script calls the microtime() function many times, which performs a request through the operating system to fetch the time.</p>
<p>Also you may notice the numbers do not quite add up to 3 seconds. This is because there were probably other processes on the server as well, and the script was not using 100% CPU for the whole duration of the 3 seconds.</p>
<p></p>
<h2><span>5. </span>Magic Constants</h2>
<p>PHP provides useful <a href="http://php.net/manual/en/language.constants.predefined.php">magic constants</a> for fetching the current line number (__LINE__), file path (__FILE__), directory path (__DIR__), function name (__FUNCTION__), class name (__CLASS__), method name (__METHOD__) and namespace (__NAMESPACE__).</p>
<p>We are not going to cover each one of these in this article, but I will show you a few use cases.</p>
<p>When including other scripts, it is a good idea to utilize the __FILE__ constant (or also __DIR__ since PHP 5.3):</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code289'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98289"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p98code289"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// this is relative to the loaded script's path</span>
<span style="color: #666666; font-style: italic;">// it may cause problems when running scripts from different directories</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config/database.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// this is always relative to this file's path</span>
<span style="color: #666666; font-style: italic;">// no matter where it was included from</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/dirname"><span style="color: #990000;">dirname</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/config/database.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Using __LINE__ makes debugging easier. You can track down the line numbers:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code290'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98290"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p98code290"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// some code</span>
<span style="color: #666666; font-style: italic;">// ...</span>
my_debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;some debug message&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__LINE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
Line 4: some debug message
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// some more code</span>
<span style="color: #666666; font-style: italic;">// ...</span>
my_debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;another debug message&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__LINE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
Line 11: another debug message
*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> my_debug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$msg</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Line <span style="color: #006699; font-weight: bold;">$line</span>: <span style="color: #006699; font-weight: bold;">$msg</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></p>
<h2><span>6. </span>Generating Unique ID&#8217;s</h2>
<p>There may be situations where you need to generate a unique string. I have seen many people use the md5() function for this, even though it&#8217;s not exactly meant for this purpose:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code291'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98291"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p98code291"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// generate unique string</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/md5"><span style="color: #990000;">md5</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>There is actually a PHP function named <a href="http://us2.php.net/manual/en/function.uniqid.php">uniqid()</a> that is meant to be used for this.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code292'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98292"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p98code292"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// generate unique string</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
4bd67c947233e
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// generate another unique string</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
4bd67c9472340
*/</span></pre></td></tr></table></div>

<p>You may notice that even though the strings are unique, they seem similar for the first several characters. This is because the generated string is related to the server time. This actually has a nice side effect, as every new generated id comes later in alphabetical order, so they can be sorted.</p>
<p>To reduce the chances of getting a duplicate, you can pass a prefix, or the second parameter to increase entropy:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code293'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98293"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p98code293"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// with prefix</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foo_'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
foo_4bd67d6cd8b8f
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// with more entropy</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</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: #666666; font-style: italic;">/* prints
4bd67d6cd8b926.12135106
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// both</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bar_'</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: #666666; font-style: italic;">/* prints
bar_4bd67da367b650.43684647
*/</span></pre></td></tr></table></div>

<p>This function will generate shorter strings than md5(), which will also save you some space.</p>
<p></p>
<h2><span>7. </span>Serialization</h2>
<p>Did you ever need to store a complex variable in a database or a text file? You do not have to come up with a fancy solution to convert your arrays or objects into formatted strings, as PHP already has functions for this purpose.</p>
<p>There are two popular methods of serializing variables. Here is an example that uses the <a href="http://php.net/manual/en/function.serialize.php">serialize()</a> and <a href="http://www.php.net/manual/en/function.unserialize.php">unserialize()</a>:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code294'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98294"><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
</pre></td><td class="code" id="p98code294"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// a complex array</span>
<span style="color: #000088;">$myvar</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'hello'</span><span style="color: #339933;">,</span>
	<span style="color: #cc66cc;">42</span><span style="color: #339933;">,</span>
	<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'two'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'apple'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// convert to a string</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/serialize"><span style="color: #990000;">serialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$myvar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
a:4:{i:0;s:5:&quot;hello&quot;;i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:&quot;two&quot;;}i:3;s:5:&quot;apple&quot;;}
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// you can reproduce the original variable</span>
<span style="color: #000088;">$newvar</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/unserialize"><span style="color: #990000;">unserialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$newvar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
Array
(
    [0] =&amp;gt; hello
    [1] =&amp;gt; 42
    [2] =&amp;gt; Array
        (
            [0] =&amp;gt; 1
            [1] =&amp;gt; two
        )
&nbsp;
    [3] =&amp;gt; apple
)
*/</span></pre></td></tr></table></div>

<p>This was the native PHP serialization method. However, since JSON has become so popular in recent years, they decided to add support for it in PHP 5.2. Now you can use the json_encode() and json_decode() functions as well:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code295'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98295"><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
</pre></td><td class="code" id="p98code295"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// a complex array</span>
<span style="color: #000088;">$myvar</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'hello'</span><span style="color: #339933;">,</span>
	<span style="color: #cc66cc;">42</span><span style="color: #339933;">,</span>
	<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'two'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'apple'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// convert to a string</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/json_encode"><span style="color: #990000;">json_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$myvar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
[&quot;hello&quot;,42,[1,&quot;two&quot;],&quot;apple&quot;]
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// you can reproduce the original variable</span>
<span style="color: #000088;">$newvar</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/json_decode"><span style="color: #990000;">json_decode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$newvar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
Array
(
    [0] =&amp;gt; hello
    [1] =&amp;gt; 42
    [2] =&amp;gt; Array
        (
            [0] =&amp;gt; 1
            [1] =&amp;gt; two
        )
&nbsp;
    [3] =&amp;gt; apple
)
*/</span></pre></td></tr></table></div>

<p>It is more compact, and best of all, compatible with javascript and many other languages. However, for complex objects, some information may be lost.</p>
<p></p>
<h2><span>8. </span>Compressing Strings</h2>
<p>When talking about compression, we usually think about files, such as ZIP archives. It is possible to compress long strings in PHP, without involving any archive files.</p>
<p>In the following example we are going to utilize the <a href="http://php.net/manual/en/function.gzcompress.php">gzcompress()</a> and <a href="http://www.php.net/manual/en/function.gzuncompress.php">gzuncompress()</a> functions: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code296'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98296"><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
</pre></td><td class="code" id="p98code296"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span>
<span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Nunc ut elit id mi ultricies
adipiscing. Nulla facilisi. Praesent pulvinar,
sapien vel feugiat vestibulum, nulla dui pretium orci,
non ultricies elit lacus quis ante. Lorem ipsum dolor
sit amet, consectetur adipiscing elit. Aliquam
pretium ullamcorper urna quis iaculis. Etiam ac massa
sed turpis tempor luctus. Curabitur sed nibh eu elit
mollis congue. Praesent ipsum diam, consectetur vitae
ornare a, aliquam a nunc. In id magna pellentesque
tellus posuere adipiscing. Sed non mi metus, at lacinia
augue. Sed magna nisi, ornare in mollis in, mollis
sed nunc. Etiam at justo in leo congue mollis.
Nullam in neque eget metus hendrerit scelerisque
eu non enim. Ut malesuada lacus eu nulla bibendum
id euismod urna sodales. &quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$compressed</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/gzcompress"><span style="color: #990000;">gzcompress</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Original size: &quot;</span><span style="color: #339933;">.</span> <a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
Original size: 800
*/</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Compressed size: &quot;</span><span style="color: #339933;">.</span> <a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$compressed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints
Compressed size: 418
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// getting it back</span>
<span style="color: #000088;">$original</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/gzuncompress"><span style="color: #990000;">gzuncompress</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$compressed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We were able to achive almost 50% size reduction. Also the functions <a href="http://www.php.net/manual/en/function.gzencode.php">gzencode()</a> and <a href="http://www.php.net/manual/en/function.gzdecode.php">gzdecode()</a> achive similar results, by using a different compression algorithm.</p>
<p></p>
<h2><span>9. </span>Register Shutdown Function</h2>
<p>There is a function called <a href="http://www.php.net/manual/en/function.register-shutdown-function.php">register_shutdown_function()</a>, which will let you execute some code right before the script finishes running.</p>
<p>Imagine that you want to capture some benchmark statistics at the end of your script execution, such as how long it took to run:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code297'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98297"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p98code297"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// capture the start time</span>
<span style="color: #000088;">$start_time</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// do some stuff</span>
<span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// display how long the script took</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;execution took: &quot;</span><span style="color: #339933;">.</span>
		<span style="color: #009900;">&#40;</span><a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start_time</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
		<span style="color: #0000ff;">&quot; seconds.&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>At first this may seem trivial. You just add the code to the very bottom of the script and it runs before it finishes. However, if you ever call the <a href="http://php.net/manual/en/function.exit.php">exit()</a> function, that code will never run. Also, if there is a fatal error, or if the script is terminated by the user (by pressing the Stop button in the browser), again it may not run.</p>
<p>When you use register_shutdown_function(), your code will execute no matter why the script has stopped running:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p98code298'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p98298"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p98code298"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$start_time</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/register_shutdown_function"><span style="color: #990000;">register_shutdown_function</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'my_shutdown'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// do some stuff</span>
<span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> my_shutdown<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$start_time</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;execution took: &quot;</span><span style="color: #339933;">.</span>
			<span style="color: #009900;">&#40;</span><a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start_time</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
			<span style="color: #0000ff;">&quot; seconds.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></p>
<h2>Conclusion</h2>
<p>Do you know any other PHP features that are not widely known but can be very useful? Please share with me in the comments. And thank you for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2010/12/25/tutorial-9-useful-php-functions-and-features-you-need-to-know/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>BRB: Finals</title>
		<link>http://gigaspartan.com/2010/12/09/brb-finals/</link>
		<comments>http://gigaspartan.com/2010/12/09/brb-finals/#comments</comments>
		<pubDate>Thu, 09 Dec 2010 04:36:26 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[Posts]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=94</guid>
		<description><![CDATA[I have finals and term papers all due next week, so I will not be posting much until that is all over. But once they are, I will be sure to most more frequently than I am now. But I would like suggestions on what to write about, so leave them below!]]></description>
			<content:encoded><![CDATA[<p>I have finals and term papers all due next week, so I will not be posting much until that is all over. But once they are, I will be sure to most more frequently than I am now. But I would like suggestions on what to write about, so leave them below! <img src='http://gigaspartan.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2010/12/09/brb-finals/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ideas: 50 Ways to Get Your Site Noticed</title>
		<link>http://gigaspartan.com/2010/12/05/ideas-50-ways-to-get-your-site-noticed/</link>
		<comments>http://gigaspartan.com/2010/12/05/ideas-50-ways-to-get-your-site-noticed/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 02:20:32 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[Ideas]]></category>
		<category><![CDATA[Marketing]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[marketing]]></category>
		<category><![CDATA[noticed]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=84</guid>
		<description><![CDATA[This article aims to show you how to attract users and make them hungry for more. Who is this post for? Anyone with a website. Not all of the items listed below will apply directly to you and your business; however, they are, at the very least, an excellent source for inspiration. 1. Write Fresh [...]]]></description>
			<content:encoded><![CDATA[<p>This article aims to show you how to attract users and make them hungry for more. Who is this post for? Anyone with a website. Not all of the items listed below will apply directly to you and your business; however, they are, at the very least, an excellent source for inspiration.</p>
<h2>1.  Write Fresh and Catchy Content</h2>
<p>  If you have good content, people will always come back for more. One of the most important things about content is to keep it fresh and up-to-date. But, just like Smashing Magazine, make sure to archive your content for people to refer back to.</p>
<p><img src="/images/dfn54m92_60htw9x8q7_b.jpg" alt="Fresh and catchy content" /></p>
<p><span id="more-84"></span></p>
<h2>2. Listen to Your Community</h2>
<p>  If you are already lucky enough to have a community—even just some regular users—listen to them. You can do this by emailing your users directly, setting up comment forms, <a href="http://www.volusion.com/livechat_software.asp">live chat</a>, or even user feedback systems such as <a href="http://uservoice.com/" target="_blank">UserVoice</a>, which allow users to vote on site issues and functionality. By listening to your community, you can determine exactly what they want.</p>
<p><img src="/images/dfn54m92_62c9qmj2cm_b.jpg" alt="Listen to your community" /></p>
<h2>3. Monitor How Your Site is Used</h2>
<p>Knowing how users use your site is vital. This allows you to target their needs better. The best way is to keep a close eye on your analytics data; check what country/area your users are coming from, what search terms are working well for you and what sites are giving you the best referrals.
</p>
<p>Find out how users navigate your site via <a href="http://www.clickdensity.com/" target="_blank">heat maps</a>. These help you alter your site so that it is easier for your visitors to use and find what they want. This keeps your users happy and more likely to return.</p>
<p><img src="/images/dfn54m92_63dx5k94f6_b.jpg" alt="Monitor how your site is used" /></p>
<h2>4. Take Part in the Online Community</h2>
<p>
When you read your favorite blog or read an article of interest, leave a comment with your name, email address and website link. Ensure you are an active member of a relevant forum discussion or networking group. Of course, make sure you are an active member of your own site’s online community sector.</p>
<p><img src="/images/dfn54m92_64hds6kcfj_b.jpg" alt="Take part in the on-line community" />  <br /><small>Smashing Magazines commenting system making it easy to take part.</small></p>
<h2>5. Submit to Directories and Galleries</h2>
<p>  This should be an ongoing process since there are many local, national and worldwide directories. Your first stop should be <a href="http://www.dmoz.org/">DMOZ.org</a>. Following this, submit to the local directories and those specializing in your site’s business sector.</p>
<p>  If you are designing web sites and are proud of your work, submit your site to web design galleries. These directories—such as <a href="http://www.cssbeauty.com/" target="_blank">CSS Beauty</a> and <a href="http://www.csselite.com/" target="_blank">CSS Elite</a>—feature beautiful and creative designs and ask users to comment on and rate each design. Once people see your work, they may become your visitors.</p>
<p><img src="/images/dfn54m92_65cgrtfmcs_b.jpg" alt="DMOZ" /></p>
<h2>6. Use Email Signatures</h2>
<p>Whenever you send out an email, make sure you have your URL in the <a href="http://www.webdevelopersnotes.com/basics/what_is_email_signature.php">email signature</a>. It’s a simple idea, but it’s  effective.</p>
<p><img src="/images/dfn54m92_66fcwft2cj_b.jpg" alt="Email signatures" /></p>
<h2>7. Have Business Cards at Hand . . . Always</h2>
<p>Ensure you always have business cards to hand out. If you meet someone needing your services,  it always pays to have a striking and memorable business card to exchange. It is worth spending time on your business card to make it <a href="http://logodesignerblog.com/creative-business-cards-design-inspiration/">creative and memorable</a>, otherwise you may just be left at the bottom of the pile with the rest.</p>
<p><img src="/images/dfn54m92_67hk6zwhd7_b.jpg" alt="Exchangnig business cards" /></p>
<h2>8. Use Company Stationery</h2>
<p>Put your website address on everything: pens, letterheads, flyers, and anything that represents your company. Make sure it is clear and pushes people to visit your web site. For example, you could say, “For more information on our new product, check ourwebsite.com”</p>
<h2>9. Win an Award</h2>
<p>
Winning an award is not easy, but it can lead to a flood of new visitors for your site. Even if you are a runner up, people will be curious and want to find out who you are. The winner of the <a href="http://www.netmag.co.uk/" title=".net magazine" target="_blank">.net magazine</a> Interactive Site of the Year in 2009, <a href="http://www.ecodazoo.com/" target="_blank">www.ecodazoo.com</a>, received huge amounts of visits from the <a href="http://www.thenetawards.com/" target="_blank">www.thenetawards.com</a> site. <a href="http://www.computerarts.co.uk/in_depth/features/ten_best_design_competitions" title="Choose your competition" target="_blank">Choose your competition</a> wisely and remember, winning is not everything, but getting your URL in front of as many readers as possible is.</p>
<p><img src="/images/dfn54m92_69dp58xbgb_b.jpg" alt="Ecodazoo" /><br />
<br /><small><a href="http://ecodazoo.com/" target="_blank">Ecodazoo</a> pushes the boundaries of 3D design on the web</small></p>
<h2>10. Run Competitions</h2>
<p>On the flip side of winning an award, running a competition can be just as beneficial to your analytics stats. This could be as simple as coming up with a new name for your latest product, or as complex as <a href="http://www.designcontest.net/">designing a new logo</a>. Either way, this will attract more users to your site and show that you are giving back to your users as well as taking.</p>
<p><img src="/images/dfn54m92_70fkzbwtdh_b.jpg" alt="The Webby Awards" /> <br /><small>The Webby Awards is the leading international award honoring excellence on the Internet.</small></p>
<h2>11. Optimize for Search Engines (SEO)</h2>
<p> Effective SEO, <a href="http://www.highrankings.com/commonsense">common sense</a> and <a href="http://www.webstandards.org/" title="web Standards" target="_blank">web standards</a> go together. The idea is to create semantic, valid code that uses the right tool for the job. This means using the right HTML tags and having a unique title and appropriate meta data for each page. Ensure search engine spiders can correctly index your site. And don’t forget that good content is a must. Remember, the most important way to climb the search engine ranks is through quality links. For more information on this, visit the <a href="http://www.google.com/support/webmasters/bin/topic.py?topic=19494"> Google Webmasters/Site owners guidelines</a>.</p>
<p><img src="/images/dfn54m92_71cv2cszmv_b.jpg" alt="Google Webmasters&#39;/Site owners&#39; guidelines." /><br />
<small><a href="http://www.google.com/webmasters/docs/search-engine-optimization-starter-guide.pdf">Search Engine Optimization Starter Guide</a></small></p>
<h2>12. Pay for Advertising</h2>
<p><a href="http://en.wikipedia.org/wiki/Pay_per_click">Pay per click advertising</a> can drive a great deal of traffic to your site, if you correctly target your keywords. Placing your banner or link on relevant sites will vastly improve the chance of you getting noticed.</p>
<p><img src="/images/dfn54m92_72cx52v5g4_b.jpg" alt="Paid advertising" /> </p>
<p><small><em>The most effective advertising gets your message to a relevant audience.</em></small></p>
<h2>13. Write Articles</h2>
<p>Writing articles for other sites as well as your own is a fantastic way to get noticed. If you sell power tools, consider writing an article about the best router for making coffee tables. Target the niche that you’re after. Just make sure you add your URL next to your name. If it’s well-written, people will want to know more about you.</p>
<p><img src="/images/dfn54m92_73djp3kdhx_b.jpg" alt="Write articles" /></p>
<h2>14. Create a Blog</h2>
<p>As you know, blogs are big and they are continuing to thrive as people want quick, useful information about a product, service or <a href="http://blog.searchenginewatch.com/090403-135206">general news</a>. <a href="http://wordpress.org/" target="_blank">WordPress</a> has been at the forefront of blogging and gives us the tools to make rich, <a href="http://www.problogger.net/archives/2005/08/15/search-engine-optimization-for-blogs/" target="_blank">search engine-friendly</a> blogs with <a href="http://sixrevisions.com/wordpress/beautiful_wordpress_designs/" target="_blank">beautiful designs</a>. Of course, it is up to you to fill your blog with fresh and catchy content.</p>
<p><img src="/images/dfn54m92_74hgqcntdt_b.jpg" alt="Amazon - recommended book for WordPress bloggers" /><br />
<a href="http://www.amazon.com/gp/product/0321591933" title="Building a WordPress Blog People Want to Read (Paperback)"><br /><small>Building a WordPress Blog People Want to Read (Paperback)</small></a></p>
<h2>15. Join Online Community Websites</h2>
<p>In the past year, we have seen the social web boom and many sites have reaped huge benefits. Creating your own Facebook page, uploading your work to <a href="http://www.deviantart.com/" target="_blank">Deviant Art</a> or creating your own <a href="http://www.squidoo.com/" target="_blank">Squidoo</a> page all help build your online presence.</p>
<p><img src="/images/dfn54m92_111dxn9nqcx_b.jpg" alt="Facebook group" /></p>
<h2>16. Get on Twitter</h2>
<p>We all know the power of Twitter by now, and much has been said on the subject of using <a href="http://twitter.com/" target="_blank">Twitter</a> as a marketing tool. But the fact remains: it is a very effective way of getting noticed. Get the most out of Twitter by choosing one of the <a href="http://www.smashingmagazine.com/2009/03/02/twitter-web-designer-and-developer-toolbox-api-and-tutorials/" target="_blank">many tools available</a>. Keep inside your niche area, follow people that matter to your website, and make it personal. Do not just spout marketing spiel or you will quickly lose followers. If you are part of a niche area consider setting up your own <a href="http://www.twibes.com/" target="_blank">Twibe</a> (a group of Twitter users) or use Twitter’s list feature.</p>
<p><img src="/images/dfn54m92_75gfgg36k5_b.jpg" alt="Twibes" /></p>
<h2>17. Record Podcasts and Videocasts</h2>
<p>
If you have something to say and you think others will be interested, then consider making a <a href="http://reviews.digitaltrends.com/guide/31/how-to-make-a-podcast" target="_blank">podcast</a> or <a href="http://en.wikipedia.org/wiki/Video_podcast" target="_blank">videocast</a>. <a href="http://www.boagworld.com/">Boagworld.com</a> has been a leader in this area for the web design field and Paul Boag has said that the majority of his new clients have come through the podcast. Here are eight tips to remember when thinking about podcasting:
</p>
<ul>
<li>
Invest in <a href="http://www.amazon.com/Samson-CO1U-USB-Condenser-Mic/dp/B000AP1RE8" title="good microphone">a</a> <a href="http://www.amazon.com/Blue-Microphones-Snowball-Microphone-White/dp/B000EOPQ7E/ref=sr_1_1?ie=UTF8&#038;qid=1287776833&#038;sr=8-1">good</a> <a href="http://www.amazon.com/Rode-Podcaster-USB-Dynamic-Microphone/dp/B000JM46FY/ref=sr_1_1?ie=UTF8&#038;qid=1287776847&#038;sr=8-1">microphone</a>.</li>
<li>Reduce background noise.</li>
<li>Prepare, but do not read a script.</li>
<li>Follow a consistent format.</li>
<li> Invite guest speakers.</li>
<li>Have regularly-scheduled podcasts.</li>
<li>Enjoy the process and relax!</li>
</ul>
<p><img src="/images/dfn54m92_109dqtq9cgk_b.jpg" alt="From the couch" /></p>
<p><a href="http://www.from-the-couch.com/" title="Video Blog of David and Marc Perel discussing their passion for Web">Video Blog of David and Marc Perel discussing their passion for Web</a></p>
<h2>18. Offer Newsletters and RSS feeds</h2>
<p>Users sign up for your newsletter and you have your captive audience. If used properly, newsletters can be very effective tools for driving people to your site. Check out <a href="http://www.smashingmagazine.com/2007/10/16/best-practices-for-bulletproof-e-mail-delivery/" title="Smashing Magazines best practices article">Smashing Magazine’s best practices article</a> before embarking on your campaign. When you are ready, use a service like <a href="http://www.mailchimp.com/" title="MailChimp">MailChimp</a> to monitor your success.
</p>
<p>
Similar to newsletters, having an RSS feed of your content is another way to keep users noticing your web site. They are easy to set up and a good way to keep your users up-to-date on your website. Make sure you keep your feeds relevant, interesting, informative and easy to find.</p>
<p><img src="/images/dfn54m92_77hr6sx9jd_b.jpg" alt="Cool RSS Icons" /><br />
<br /><small>Ref: <a href="http://www.hongkiat.com/blog/really-cool-rss-feed-icons" target="_blank">http://www.hongkiat.com/blog/really-cool-rss-feed-icons/</a></small></p>
<h2>19. Send Seasonal Cards and E-Cards</h2>
<p>Send out cards to your clients either by snail-mail or email. This is not only a nice gesture but also keeps your website address firmly imprinted in their mind. Using services like <a href="http://www.cardsdirect.com/" title="Cards Direct">Cards Direct</a> is a quick and easy way to spread the holiday cheer with your website address attached to it.</p>
<h2>20. Run an Event</h2>
<p> Running an event is a fantastic way to both market your business and connect with your customers, clients and experts in your sector. For example, the <em>Future of Web Design</em> events are run all around the world featuring many professionals from the web design community. Your event does not have to be about web design; it can be focused on your site and your business’ sector, in order to to raise awareness of you and your site.</p>
<p>  Alternatively, you can be one of the speakers. This form of marketing has worked very well for people like Brett Welch, Danny Somekh and Folkert Gorter who spoke at the <a href="http://events.carsonified.com/fowd/2009/london/content">FOWD London 2009</a>.</p>
<p><img src="/images/dfn54m92_784c4g22d5_b.jpg" alt="Carsonified events" /></p>
<h2>21. Sell Branded T-shirts</h2>
<p>Do your own marketing: have t-shirts, bags, folders and other items sporting your website address clearly.</p>
<p><img src="/images/dfn54m92_79dqts8gk8_b.jpg" alt="Joomla T-Shirts" /><br />
<br /><small>Joomla raises peoples awareness with its <a href="http://shop.joomla.org/" title="online shop">online shop</a>.</small></p>
<h2>22. Create a Mobile App</h2>
<p>Can your service be provided via a mobile app? Would customers want to purchase your product on the go? If so, consider creating your own mobile application. If you want to tap into the vast iPhone / iPad market, you can consider selling your app on the <a href="http://www.apple.com/iphone/apps-for-iphone/">iPhone app store</a>. You can <a href="http://mobile.tutsplus.com/">create the app yourself</a> or  use a company like <a href="http://www.siruna.com/">Siruna</a> to create the mobile application for you.</p>
<p><img src="/images/dfn54m92_80hb3845fs_b.jpg" alt="App Store" /></p>
<h2>23. Help with Charity Work</h2>
<p>  You can donate directly to a charity or run a charity event. There are <a href="http://www.charitychoice.co.uk/" target="_blank">many charities</a> out there, so choose wisely and carefully.</p>
<p><img src="/images/dfn54m92_81d2kg4wdb_b.jpg" alt="Oxfam" /></p>
<h2>24. Sponsor a College and University Project</h2>
<p>Many courses require a student to get a set amount of work experience or do a final year project. Contact your local college and/or university and offer them a project that you would like their students to help on. My final year project was to create a new search engine-optimized template for a local business directory. The directory benefited from the new template tremendously and we learned a great deal as a team.</p>
<p>Submitting proposals for college and university projects not only supports your website, but it also helps students gain real-life experience in their chosen field. Here are a few examples:</p>
<ul>
<li><a href="http://www.business-services.salford.ac.uk/" title="Business Services at the University of Salford" target="_blank">Business Services at the University of Salford UK</a></li>
<li><a href="http://cbid.bme.jhu.edu/submit-a-project/index.php" title="Center for Bioengineering Innovation &amp; Design" target="_blank">Center for Bioengineering Innovation &amp; Design</a> </li>
<li><a href="http://www.its.monash.edu.au/staff/projects/project-management/sponsoring.html" title="Monash University Australia" target="_blank">Monash University Australia</a> </li>
</ul>
<p><img src="/images/dfn54m92_82c5svbnc2_b.jpg" alt="College and University Project Sponsorship" /></p>
<h2>25. Place Your Content Elsewhere</h2>
<p>Your website is not the only place to put your content. If you sell a product, you can also list it on eBay. If you are a band, why not put your songs on <a href="http://www.last.fm/uploadmusic">Last.fm</a>? If your site  publishes tutorials, you can also submit a tutorial to other sites. Once people notice your content they will be more inclined to visit your site, hungry for more.</p>
<p><img src="/images/dfn54m92_83ggd26k4f_b.jpg" alt="Last.fm" /></p>
<h2>26. Give Away Free Stuff</h2>
<p>In the case of Vista Print, they offer <a href="http://www.vistaprint.co.uk/vp/ns/studio3.aspx?pf_id=064&#038;combo_id=4298&#038;gallery_id=64&#038;category_id=11" title="free business cards">free business cards</a>; the cards have your logo and details on one side and theirs on the other. Here is a list of ideas you can give away:</p>
<ul>
<li><a href="http://www.print4half.com/promotional-products-product-7.aspx" title="Pens, badges and other stationary">Pens, badges and other stationary</a> </li>
<li>  Free <a href="http://savethepixel.org/" title="e-book">e-books</a> or even <a href="http://www.audiobooks.org/" title="audio book">audio books</a> </li>
<li>  <a href="http://www.apple.com/downloads/macosx/icons_screensavers/" title="Screensavers">Screensavers</a> </li>
<li>  <a href="http://www.lemonjelly.ky/downloads.html" title="Desktop backgrounds">Desktop backgrounds</a> </li>
<li><a href="http://www.amazon.co.uk/gp/gc" title="Vouchers">Amazon Vouchers</a></li>
</ul>
<p><img src="/images/dfn54m92_85dtjwnspt_b.jpg" alt="Vista print" /></p>
<h2>27. Sponsor Something</h2>
<p>Sponsorship can be a remarkable way of getting your website in front of hundreds or thousands of eyes. When choosing a sponsorship project, keep to ones that will reflect well on your company as well as being relevant. For more information check out the following sites in the <a href="http://www.sponsorship.com/" target="_blank">UK</a> and <a href="http://www.sponsorscape.com/nacntc.htm">North America.</a> You can also place advertisements stating that you or your company is looking for sponsorship proposals from individuals or companies. When you receive the proposals, have a set criteria of what you’re looking for in mind.</p>
<p><img src="/images/dfn54m92_86r4xwk3d2_b.jpg" alt="Datapower Tools five aside football team" /><br />
<br /><small><a href="http://www.datapowertools.co.uk/" title="DataPowertools.co.uk">The DataPowertools.co.uk</a> 5-a-side football team. </small></p>
<h2>28. Get in the News</h2>
<p>The news takes many forms in today’s media; one of the most beneficial ways to increase awareness of your site is to get it featured on a popular news website. One of the top five news portals is <a href="http://news.google.com/" target="_blank">Google News</a>. You should concentrate on having original content, multiple authors, proper attribution, and good response time. For more information, check out the Website Magazine article “<a href="http://www.websitemagazine.com/content/blogs/posts/pages/Get-Your-Website-Included-in-Google-News.aspx">Get Your Website Included in Google News</a>“.</p>
<p><img src="/images/dfn54m92_87xpbq3s4z_b.jpg" alt="Man reading the news" /></p>
<h2>29. Release Press Releases</h2>
<p>Do you have web site that people are interested in on a national or local level? Why not submit a press release informing the public about your new site? Simply place an ad in the local paper or a sector-specific magazine.</p>
<h2>30. Sell Merchandise</h2>
<p>Do you have a product that you think will sell? Why not try your hand at merchandising?  If you are new to all this, you can use services like <a href="http://www.50below.info/merchandising.htm" target="_blank">50below.com</a> to help setup your online store. If you do have a number of products, list them on various other platforms like <a href="http://pages.ebay.com/SERVICES/BUYANDSELL/WELCOME.HTml">eBay</a>, <a href="http://www.google.com/products">Froogle</a>, and <a href="http://www.amazon.com/gp/seller/sell-your-stuff.html">Amazon</a> with links back to your site within the product descriptions.</p>
<p><img src="/images/dfn54m92_88dq86vngc_b.jpg" alt="50 Below" /></p>
<h2>31. Solve a Problem</h2>
<p>Find a problem that affects your website’s sector, create a fix, and publish it. A good solution to a difficult problem is a surefire way to get people flocking to your website. One example is the <a href="http://homepage.ntlworld.com/bobosola/" target="_blank">PNG fix for Internet Explorer</a>; this simple site received thousands of hits as it helped solve a problem with image transparencies in IE6.</p>
<p><img src="/images/dfn54m92_893xfrbvcn_b.jpg" alt="PNG fix" /><br />
<br /><small><a href="http://labs.unitinteractive.com/unitpngfix.php" target="_blank">http://labs.unitinteractive.com/unitpngfix.php</a></small></p>
<h2>32. Add Widgets to your Website</h2>
<p> Give your site a boost by adding <a href="http://homepage.ntlworld.com/bobosola/" target="_blank">news feeds</a>, <a href="http://www.bloglines.com/" target="_blank">blogs</a>, <a href="http://twitterfeed.com/" target="_blank">tweets</a>, <a href="http://www.widgetbox.com/" target="_blank">widgets</a>, <a href="http://delicious.com/" target="_blank">Delicious links</a>, <a href="http://digg.com/add-digg" target="_blank">Diggs</a> and <a href="http://www.stumbleupon.com/" target="_blank">StumbleUpon links</a> to your site. For the most comprehensive list of widgets, try the <a href="http://eco.netvibes.com/widgets">netvibes ecosystem</a>. When adding widgets to your website, keep in mind that they should enhance the user experience and not clutter your website. <a href="http://onecoolsite.wordpress.com/2009/01/02/widgets-less-is-more/" target="_blank">Less is more</a>!</p>
<p><img src="/images/dfn54m92_90d6bfx5fq_b.jpg" alt="Widgetbox" /><br />
<a href="http://www.widgetbox.com/" title="Widgetbox gives you more than enough choice of widgets" target="_blank"><br /><small>Widgetbox gives you more than enough choice of widgets</small></a></p>
<h2>33. Publish and Share YouTube videos</h2>
<p>We all know that YouTube is massive. One of the biggest ways that websites get noticed is by uploading or creating <a href="http://www.youtube.com/watch?v=QvJoc8oEays&amp;feature=fvst">creative</a>, <a href="http://www.youtube.com/watch?v=VKKMB9vRkIg">fun</a>, <a href="http://www.youtube.com/watch?v=mC3C1aVOOWA&amp;NR=1">shocking</a>, <a href="http://www.youtube.com/watch?v=l5YYuLJxWQ0&amp;feature=related">interesting</a>, or downright <a href="http://www.youtube.com/watch?v=v91nKja2Qw4">weird</a> videos. Setting up your own channel will allow users to subscribe to it, thus giving you more eyes on your content and website URL. Making a <a href="http://www.youtube.com/watch?v=wIr6AzQuW38">short introduction</a>, or a series of clips like <a href="http://www.youtube.com/user/expertvillage">Expert Village</a> is a cost-effective way of driving traffic to your site, as well as climbing the <a href="http://ezinearticles.com/?Add-Video-to-Website-to-Improve-Your-Search-Engine-Ranking&amp;id=2298189">search engine results ladder</a>.</p>
<p><img src="/images/dfn54m92_91d898kgcm_b.jpg" alt="Expert Village" /></p>
<h2>34. Offer Free Services</h2>
<p>  Having a specific free service that is useful and intuitive could attract a plethora of users. Here are some examples of sites with free tools and services:</p>
<ul>
<li><a href="http://freelanceswitch.com/rates/" title="Freelancer rates calculator">Freelancer rates calculator</a> </li>
<li><a href="http://kuler.adobe.com/" title="Kuler colour scheme creator">Kuler colour scheme creator</a> </li>
<li><a href="http://browsershots.org/" title="Multiple browser tests">Multiple browser tests</a> </li>
<li><a href="http://www.xml-sitemaps.com/" title="XML site map generator">XML site map generator</a> </li>
<li><a href="http://hotjobs.yahoo.com/salary" title="Calculate your salary">Calculate your salary</a> </li>
<li><a href="http://www.xe.com/ucc/customize.php" title="Customized currency converter">Customized currency converter</a> </li>
<li><a href="http://www.websiteoptimization.com/services/analyze/" title="Web Page Analyzer">Web Page Analyzer</a> </li>
<li><a href="http://tools.dynamicdrive.com/favicon/" title="FavIcon Generator">FavIcon Generator</a> </li>
<li><a href="http://instantdomainsearch.com/" title="Instant Domain Search">Instant Domain Search</a></li>
</ul>
<p><img src="/images/dfn54m92_92h8cx3vgk_b.jpg" alt="Rates Calculator" /><br />
<br /><small><a href="http://freelanceswitch.com/rates/" title="Freelance Switch&#39;s rate helps people work out their standard freelance rate."> Freelance Switch’s rate calculator helps people work out their standard freelance rate.</a></small></p>
<h2>35. Solicit Consultation Services</h2>
<p>If you are a specialist in your field, consider offering <a href="http://en.wikipedia.org/wiki/Online_consultation" target="_blank">consultation services</a>. A consultation service can be done via the internet, over the phone, or in person. You can provide this service to individuals, groups, or businesses; and with the power of <a href="http://www.skype.com/intl/en/business/" target="_blank">Skype</a> and a web cam, it can be just as though you are standing next to them. You could even offer alternative consultation services, like usability testing.</p>
<p>What tools are the professional online consultants using? Many online consultants use <a href="http://www.skype.com/" target="_blank">Skype</a> because it is cheap, effective and well-supported. When using any peer-to-peer communications service, make sure you set-up a separate account for your consultation service so you keep your private messaging private. Skype offers a <a href="http://skypeprime.skype.com/" target="_blank">Skype Prime</a> account where you can register as a trainer and people can contact you for one-on-one or group learning.</p>
<p><img src="/images/dfn54m92_93hdm7brdw_b.jpg" alt="Skype" /></p>
<h2>36. Make Affiliate Schemes</h2>
<p>As a business, you offer rewards to affiliate marketers for every visit or consumer brought to the site using <a href="http://www.sugarrae.com/how-to-survive-the-affiliate-evolution/" target="_blank">affiliate marketing techniques</a>. This method allows you to attract more customers to your site by letting someone else do the hard work. You can offer different <a href="http://en.wikipedia.org/wiki/Affiliate_marketing#Types_of_affiliate_websites" target="_blank">types of schemes</a> such as co-branding, white labeling, loyalty schemes and <a href="http://www.associateprograms.com/directory/" target="_blank">many more</a>.</p>
<p>There are hundreds of thousands of schemes available, so make yours attractive to marketers by offering high rewards for sales.</p>
<p><img src="/images/dfn54m92_94476vwzcw_b.jpg" alt="Affiliate programs" /><br />
<small><a href="http://www.affiliateprograms.com/forum/">Affiliate Programs forum</a> contains information about hundreds of schemes and—most importantly—user feedback from people using them.</small></p>
<h2>37. Give out Vouchers and Special Offers</h2>
<p>If you have a marketing budget, spend some time attracting users with goodies. Maybe you could offer 10% off their next subscription, or a free trial to your service for 30 days. Vouchers can also attract users as you can post your voucher on one of the <a href="http://www.associateprograms.com/directory/" target="_blank">many voucher sites.</a></p>
<p><img src="/images/dfn54m92_95fj34mdf2_b.jpg" alt="Special Offers" /><br />
<br /><small>Make your special offer loud and proud so people notice.</small></p>
<h2>38. Become a Reseller</h2>
<p>A reselling service is where you sell an existing product or service such as <a href="http://www.unidoxhosting.com/" target="_blank">hosting</a> and <a href="http://www.unidoxhosting.com/page/reseller" target="_blank">domain names</a>. Reselling is a way to gain<a href="http://notbythehour.com/" target="_blank"> passive income</a> by offering a quality product or service you would not be able to develop alone. This ability to give a better service will attract customers. If you ensure good customer service you might benefit from <a href="http://en.wikipedia.org/wiki/Word_of_mouth" target="_blank">word of mouth</a> recommendations.</p>
<p><img src="/images/dfn54m92_96dmndnzcv_b.jpg" alt="HostGator" /></p>
<p><small><a href="http://unidoxhosting.com/">Unidox Hostings’s reseller packages offer a lot of choice.</a></small></p>
<h2>39. Have a Great Idea</h2>
<p>The internet has been a veritable breeding ground for creative people and ideas. This creativity has given birth to some amazing ideas that make people flock to the site. If you think you have a <a href="http://www.dailybits.com/11-undiscovered-website-ideas-to-steal-and-make-you-rich/" title="great idea">great idea</a>, harness it. Turn your idea into a reality, like Google’s originators Larry and Sergey, who created a fast and simple search environment called Backrub in 1995. Don’t forget the <a href="http://www.milliondollarhomepage.com/" title="student who sold pixels to put himself through University in 2004">student who sold pixels to put himself through University in 2004</a>!</p>
<p><img src="/images/dfn54m92_110dxm5b576_b.jpg" alt="1,000,000 pixels" /></p>
<p><small>&#8220;The main motivation for doing this is to pay for my degree studies.&#8221;</small></p>
<h2>40. Create a Plug-in, Template or Widget</h2>
<p>A widget or <a href="http://en.wikipedia.org/wiki/Plugin">plug-in</a> is a design to enhance a platform. It is designed to entertain or make life easier for users. Keep this in mind when developing. For example, you can make a WordPress plugin or Joomla template. Make sure people know who made it and where they can find your website.</p>
<h2>41. Write Tutorials</h2>
<p>If you are an expert in your field and you know what your talking about, choose a subject and write a tutorial on it. You can then make the tutorials available on your own site (to capture your audience), or <a href="http://hackspc.com/where-to-submit-your-tutorials/" target="_blank">submit your tutorial</a> to other sites.</p>
<h2>42. Write a Book</h2>
<p>Writing a book is <a href="http://lifelearningtoday.com/2007/05/21/how-to-write-a-book-in-60-days-or-less" target="_blank">time-consuming</a> and it is a labour of love, but it can reap so many benefits as a product in its own right. It will raise awareness for its topic and author. Books like Elliot Jay Stock’s <a href="http://www.sitepoint.com/books/sexy1/" target="_blank">Sexy Web design</a>, Steve Krug’s <a href="http://www.amazon.com/Dont-Make-Me-Think-Usability/dp/0321344758/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1243151767&amp;sr=1-1" target="_blank">Don`t Make me Think</a> or even an audio book like <a href="http://en.wikipedia.org/wiki/Chris_Anderson_%28writer%29">Chris Anderson</a>‘s “The Long Tail” have brought brand awareness and visitors to each of the authors’ respective websites.</p>
<p><img src="/images/dfn54m92_99cnqnrpf5_b.jpg" alt="Books" /></p>
<h2>43. Start a Magazine or Online Publication</h2>
<p>Blogging is a simplified version of creating an <a href="http://en.wikipedia.org/wiki/Online_magazine" target="_blank">online magazine</a>. Your online magazine should be much more by concentrating on different types of content, <a href="http://www.inspirationbit.com/how-to-build-lasting-relationships-with-your-readers/" target="_blank">building a community of readers</a>, and exploiting various <a href="http://www.webmarketingplus.co.uk/ecommerce/revenue_streams.html">revenue streams</a>.</p>
<p>When creating an online magazine make sure you know your CMS inside out. It will be the foundations of your business and web site and should allow you to make publishing the content painless. Think about how you will <a href="http://www.ehow.com/how_2256926_start-profitable-online-magazine.html">make the money</a> to help you grow your site. Newsletters, RSS, and forums will help you keep your readers up-to-date and talking.</p>
<p>If you need help with content you can look at <a href="http://www.associatedcontent.com/">purchasing content</a> or invite other authors to help you get started.</p>
<p><img src="/images/dfn54m92_100k53vz3ch_b.jpg" alt=".net" /></p>
<h2>44. Launch a Subscription Service</h2>
<p>Subscription services can help you gain a constant stream of visitors to your site as they download the latest news, music or tutorials.</p>
<p>If you are planning on charging for subscription, use an integrated payment system like <a href="http://spreedly.com/" target="_blank">Spreedly</a> to take the pain of setting up payment gateways and storing credit card details away.</p>
<p><img src="/images/dfn54m92_101cnqff3hc_b.jpg" alt="Spreedly" /></p>
<h2>45. Attend Networking Events</h2>
<p>  Networking events can be a fun and effective way to promote your website. Armed with your business cards and an approachable smile, you are sure to meet someone who is interested in your services or who knows someone else who will be.</p>
<p>The key is in <a href="http://www.5min.com/Video/How-to-A-Choose-a-Networking-Event-112855666">choosing the right event</a> for you and <a href="http://www.business.com/guides/preparing-for-networking-events-1264/" target="_blank">being prepared</a>. Networking events are normally based around certain <a href="http://www.londonb2b.net/index.php">types of businesses</a> or they target professionals in <a href="http://www.bangkoknetworking.com/" target="_blank">specific areas</a>. Choose what is best for you. Check online event sites like <a href="http://www.facebook.com/apps/application.php?id=2344061033">Facebook</a>,<a href="http://www.meetup.com/">Meet-up</a> or the local paper. If you can`t find one in your area, <a href="http://www.contact25.com/" target="_blank">run your own</a>!</p>
<p><img src="/images/dfn54m92_102cvxzxk5t_b.jpg" alt="Networking event" /></p>
<h2>46. Push Out Product Updates</h2>
<p>  Offering regular updates to your product can attract a consistent stream of users wanting to benefit from the new update. You should notify your users when a new update is available; this can be done via the software itself or via your website. Consider making your updates a regular attraction and remember to make them easy to download and install.</p>
<p><img src="/images/dfn54m92_103hsmpxjhd_b.jpg" alt="Firefox update" /><br /><small>FireFox makes it simple to update by making it part of the software. The user does not have to go away, download and install the update, but simply click “OK.”</small></p>
<h2>47. Label Your Work</h2>
<p>Be proud of your work and put your logo and web site address on it. For web designers, write in your contract that the finished site must feature a back-link to your site. This may not always be possible, but anyone enjoying the site you built will be intrigued to see the company who built it. For photographers, put a water mark on all your site samples; if you are a producer, have your website address on your packaging. No matter what sector you are in, there is always room for a label on your work.</p>
<p><img src="/images/dfn54m92_104crng4pcj_b.jpg" alt="Label your work" /> <br /><small>Links to a sites design company can normally be found on the footer.</small></p>
<h2>48. Use White Label Sites</h2>
<p>  A lot of sites, especially in the hotel and leisure industry, use what is called a “White Label” site. This enables a successful brand to offer a service without having to build the functionality and databases itself. You can go about this two ways:</p>
<ol>
<li>Use a white label service such as <a href="http://www.whitelabeldating.com/" title="white label dating">white label dating</a>, <a href="http://socialspring.com/" title="social networking">social networking</a>, <a href="http://www.lastminute.com/site/help/about_us/partner-network.html" title="holiday and lieasure">holiday and leisure</a> to create a site. For more information on white labeling check out this <a href="http://www.web-strategist.com/blog/2007/02/12/list-of-white-label-social-networking-platforms/" title="article">article</a>. </li>
<li>Allow users to use your functionality and database to create their own white label site. <a href="http://www.lastminute.com/" title="lastminute.com">Lastminute.com</a> , has created a very successful net of sites that utilize their data and functionality.</li>
</ol>
<p><img src="/images/dfn54m92_1063vv4b7cq_b.jpg" alt="Last minute" /><br />
<br /><small>Sites using Lastminute’s database / functionality can be seen on.</small></p>
<h2>49. Hide a Konami Code Easter Egg</h2>
<p>  A new sensation on the web is the addition of hidden code; when users press a sequence of keys, they get a hidden surprise (this is called <a href="http://en.wikipedia.org/wiki/Easter_egg_(media)">easter egg code</a>). <a href="http://www.sitepoint.com/blogs/2009/05/12/does-your-site-have-the-konami-code/">Sitepoint</a> and many other sites have done articles on this “blast from the past” way of entertaining your users. For more information go to <a href="http://konamicodesites.com/"><b>http://konamicodesites.com/</b></a>; you will need the code to access it: <em>up, up, down , down, left, right, left, right, b, a. </em>Enjoy!</p>
<p><img src="/images/dfn54m92_107ggnnjc2z_b.jpg" alt="Konami game" /> <br /><small> You’ll need quick reactions to score on this Konami game!</small></p>
<h2>50. Use Social Bookmarking</h2>
<p>  The last tip is also one of the best and easiest. Stemming from the simple idea of having a &#8220;Tell a friend&#8221; form on your website, allow users to bookmark your site on the many social bookmark sites. That way, you are letting other popular sites do the hard work for you. <a href="http://en.wikipedia.org/wiki/Social_bookmarking">Social bookmarking</a> meta-services like <a href="http://www.addtoany.com/" target="_blank">AddToAny</a> are a simple way for users to publish your site to their accounts on <a href="http://www.facebook.com/" target="_blank">Facebook</a>, <a href="http://delicious.com/" target="_blank">Delicious</a>, <a href="http://www.stumbleupon.com/" target="_blank">StumbleUpon</a> and many more with one click. Copy and paste their code on to your pages and you’re away.</p>
<p><img src="/images/dfn54m92_108jqh9s6ch_b.jpg" alt="Social Bookmarking Icons" /></p>
<p>That wraps it up for this article; I hope you have enjoyed the read. Some of the ideas are a little wacky, but they will all help you get your site noticed.</p>
<p>I look forward to reading your feedback!</p>
<p><strong>Like the change in posts? Dont like it? Let me know!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2010/12/05/ideas-50-ways-to-get-your-site-noticed/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial: File Operations in PHP</title>
		<link>http://gigaspartan.com/2010/12/02/tutorial-file-operations-in-php/</link>
		<comments>http://gigaspartan.com/2010/12/02/tutorial-file-operations-in-php/#comments</comments>
		<pubDate>Thu, 02 Dec 2010 06:20:12 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[copying]]></category>
		<category><![CDATA[deleting]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[file operations]]></category>
		<category><![CDATA[moving]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=65</guid>
		<description><![CDATA[Today we will learn how to work with file operations using PHP. This is one of the most fundamental subjects of server side programming in general. Files are used in web applications of all sizes. So let&#8217;s learn how to read, write, create, move, copy, delete files and more. 1. Copying, Moving and Deleting Files [...]]]></description>
			<content:encoded><![CDATA[<p>Today we will learn how to work with file operations using PHP. This is one of the most fundamental subjects of server side programming in general. Files are used in web applications of all sizes. So let&#8217;s learn how to read, write, create, move, copy, delete files and more.</p>
<h2>1. Copying, Moving and Deleting Files</h2>
<p>We are going to start learning with these operations first, because they are very simple one liners, and they do not involve dealing with file pointers.</p>
<h4>Copying a file</h4>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code326'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65326"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p65code326"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$new_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test_copy.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/copy"><span style="color: #990000;">copy</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newfile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
<span id="more-65"></span></p>
<h4>Moving a File</h4>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code327'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65327"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p65code327"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$new_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test_copy.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/rename"><span style="color: #990000;">rename</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newfile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Note that the function is called &#8220;rename&#8221; and not &#8220;move&#8221;. Internally, renaming a file is the same as moving it to a new location.</p>
<p>Same code works when changing the directory of the file:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code328'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65328"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p65code328"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$new_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;different/folder/test_copy.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/rename"><span style="color: #990000;">rename</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newfile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h4>Deleting a File</h4>
<p>There is no such function as &#8220;delete&#8221;. Here we call the &#8220;unlink&#8221; function.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code329'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65329"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p65code329"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/unlink"><span style="color: #990000;">unlink</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newfile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h2>2. Opening and Reading a File</h2>
<p>Now it is time to start dealing with some file pointers. There is more than one way of reading the contents of a file in PHP, but we are going to start with the most basic method first. Later in the article we will learn about the alternative methods.</p>
<p>There are three file functions we will use here: fopen, fread and fclose.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code330'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65330"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p65code330"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// open the file</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// read 5k data from the file</span>
<span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fread"><span style="color: #990000;">fread</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// close the file</span>
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>When opening the file, we include a second parameter, which determines the type of access. In this case &#8220;r&#8221; stands for &#8220;read only&#8221;. There are other types that we need to use for writing and appending that we will cover later in the article.</p>
<p>The second important thing to pay attention to is the second parameter in the fread function. This determines the length of the data (in bytes) to read from the file. To read the entire contents of the file, we need to pass the total file size. The filesize function does the trick:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code331'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65331"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p65code331"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// open the file</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// read 5k data from the file</span>
<span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fread"><span style="color: #990000;">fread</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// close the file</span>
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h2>3. Understanding File Pointers</h2>
<p>Since we started working with file pointers, let&#8217;s take a moment and try to understand them better.</p>
<p>A file pointer, also known as a file &#8220;handle&#8221;, is a variable that refers to a file. The exact value of this variable is unimportant; all we need to know is that it points to a specific file. The pointer is usually obtained by opening a file. In PHP we use the <a href="http://us.php.net/manual/en/function.fopen.php">fopen</a> function.</p>
<p>Even though PHP has an excellent garbage collector that closes all open file pointers at the end of a scripts execution, it is considered a good practice to close them manually using the <a href="http://us.php.net/manual/en/function.fclose.php">fclose</a> function.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code332'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65332"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p65code332"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// always close files</span>
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h4>Position in a File</h4>
<p>The file pointer actually does more than just pointing to a file. It also points to a specific position inside that file. In most cases, when you open a file, the file pointer points to the beginning (or position 0) in that file. We will cover this concept in more detail when we talk about &#8220;seeking&#8221; later in the article.</p>
<h2>4. Writing To a File</h2>
<p>Just like reading files, there is more than one way of writing to a file. First we will see the basic way, and later in the article we will look at alternatives.</p>
<p>The following code writes the contents of the $data variable into the test.txt file.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code333'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65333"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p65code333"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0123456789abc&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// open the file for writing</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// write data to the file</span>
<a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// close the file</span>
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This time we used a different flag for opening the file. The &#8220;w&#8221; flag opens the file for writing, and overwrites all existing data in the file. If there was anything in the test.txt file, it will be replaced with the string in $data.</p>
<p>The <a href="http://us.php.net/manual/en/function.fwrite.php">fwrite</a> function was very simple, we just passed the file pointer and the data to be written to the file. There is an optional third parameter that determines the length of data to be written:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code334'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65334"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p65code334"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0123456789abc&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// open the file for writing</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// write data to the file (only 5 bytes)</span>
<a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</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: #666666; font-style: italic;">// close the file</span>
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If you run the code above, the contents of test.txt will only be &#8220;01234&#8243;.</p>
<h4>Creating a New File</h4>
<p>The examples above also work for creating new files. If the file test.txt does not exist, it will be created automatically, just by the fopen function call.</p>
<p>If all you want to do is to create a new blank file, without writing any data in it, this code will work:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code335'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65335"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p65code335"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;does_not_exist.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// creates and opens the file</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// close the file</span>
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h2>5. Seeking</h2>
<p>The act of moving the file pointers location is called &#8220;seeking&#8221;. To seek we can use the function <a href="http://us.php.net/manual/en/function.fseek.php">fseek</a>, and to get the position of a given pointer, we can use the function <a href="http://us.php.net/manual/en/function.ftell.php">ftell</a>.</p>
<p>Before we use seeking, let&#8217;s create a file with 10 bytes of data in it.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code336'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65336"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p65code336"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0123456789&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now let&#8217;s see how these functions behave. Here are a few different examples:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code337'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65337"><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
</pre></td><td class="code" id="p65code337"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// opening for reading, pointer should be at 0</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;open: &quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// outputs 0</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/ftell"><span style="color: #990000;">ftell</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// -----</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// seek to byte 4</span>
<a href="http://www.php.net/fseek"><span style="color: #990000;">fseek</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;seek 4: &quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// outputs 4</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/ftell"><span style="color: #990000;">ftell</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// -----</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// you can go out of bounds</span>
<a href="http://www.php.net/fseek"><span style="color: #990000;">fseek</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;out of bounds: &quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// outputs 9000</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/ftell"><span style="color: #990000;">ftell</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ----</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// this is how you get to the end</span>
<a href="http://www.php.net/fseek"><span style="color: #990000;">fseek</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> SEEK_END<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;end: &quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// outputs 10</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/ftell"><span style="color: #990000;">ftell</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ----</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// move relative to current position</span>
<span style="color: #666666; font-style: italic;">// negative numbers work</span>
<a href="http://www.php.net/fseek"><span style="color: #990000;">fseek</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> SEEK_CUR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;go back by 3: &quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// outputs 7</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/ftell"><span style="color: #990000;">ftell</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ----</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// or seek relative to the end</span>
<span style="color: #666666; font-style: italic;">// again, negative works</span>
<a href="http://www.php.net/fseek"><span style="color: #990000;">fseek</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> SEEK_END<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;go back by 3 from the end: &quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// outputs 7</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/ftell"><span style="color: #990000;">ftell</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ----</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// does the same thing as seeking to 0</span>
<a href="http://www.php.net/rewind"><span style="color: #990000;">rewind</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;rewind: &quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// outputs 0</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/ftell"><span style="color: #990000;">ftell</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h4>Other Operations Moving File Pointer Position</h4>
<p>The file pointer position can move by operations other than fseek.</p>
<p>For example, reading moves the position:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code338'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65338"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p65code338"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// reading 6 bytes</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fread"><span style="color: #990000;">fread</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// outputs 6</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/ftell"><span style="color: #990000;">ftell</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Writing also moves it:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code339'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65339"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p65code339"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0123456789&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// writing 10 bytes</span>
<a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// outputs 10</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/ftell"><span style="color: #990000;">ftell</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h2>6. File Access Modes</h2>
<p>Now that we are familiar with the reading, writing and seeking concepts, it is time better understand the different file access modes. When opening a file with the <a href="http://us.php.net/manual/en/function.fopen.php">fopen</a> function, the second parameter must be provided, which is the file access mode. There is a table in the PHP manual describing them:</p>
<p><img src="http://img140.imageshack.us/img140/1543/59830798.png" alt="" /></p>
<p>By looking at these, you can see that we have more options than we talked about so far. Here are some important factors to consider about picking the right option:</p>
<ul>
<li>Opening with &#8216;r+&#8217; will fail, if the file is not writable, even if you just want to read and not write to it.</li>
<li>Both &#8216;w&#8217; and &#8216;w+&#8217; will completely delete the contents of an existing file as soon as you open it. If you intend to add data, you need to use &#8216;a&#8217; or &#8216;a+&#8217;.</li>
<li>If you want to create new files, and also prevent accidentally overwriting and existing file, use &#8216;x&#8217; or &#8216;x+&#8217;.</li>
<li>When working with binary files, such as images, add the letter &#8216;b&#8217; after the mode. For example: &#8216;rb&#8217; or &#8216;r+b&#8217;</li>
</ul>
<h2>7. Getting File Information</h2>
<p>There are many pieces of information we can obtain about a file besides just its contents (size, last access time, modify time etc&#8230;) The main function used for this is <a href="http://us.php.net/manual/en/function.stat.php">stat</a>.</p>
<p>It returns the information in an array:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code340'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65340"><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
</pre></td><td class="code" id="p65code340"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/stat"><span style="color: #990000;">stat</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* prints:
&nbsp;
Array
(
    [0] =&gt; 2
    [1] =&gt; 0
    [2] =&gt; 33206
    [3] =&gt; 1
    [4] =&gt; 0
    [5] =&gt; 0
    [6] =&gt; 2
    [7] =&gt; 10
    [8] =&gt; 1264374556
    [9] =&gt; 1264374556
    [10] =&gt; 1264373231
    [11] =&gt; -1
    [12] =&gt; -1
    [dev] =&gt; 2
    [ino] =&gt; 0
    [mode] =&gt; 33206
    [nlink] =&gt; 1
    [uid] =&gt; 0
    [gid] =&gt; 0
    [rdev] =&gt; 2
    [size] =&gt; 10
    [atime] =&gt; 1264374556
    [mtime] =&gt; 1264374556
    [ctime] =&gt; 1264373231
    [blksize] =&gt; -1
    [blocks] =&gt; -1
)
&nbsp;
*/</span></pre></td></tr></table></div>

<p>The data is in 12 pieces and is repeated, first with numeric keys, and then again with string keys.</p>
<p>The <a href="http://us.php.net/manual/en/function.stat.php">PHP manual</a> has a table explaining each of them as shown above.</p>
<h2>8. Alternative Functions</h2>
<p>As I mentioned earlier, there are other alternative functions that can be used for reading and writing files. Let&#8217;s go over some of them.</p>
<h4>file_get_contents</h4>
<p>This function returns the entire contents of a given file. Also you do not need to deal with file pointers at all.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code341'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65341"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p65code341"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/file_get_contents"><span style="color: #990000;">file_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The function accepts four more additional parameters as described in the <a href="http://us.php.net/manual/en/function.file-get-contents.php">PHP manual</a>. For example, to read only a portion of the file, you can set the fourth and fifth parameters:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code342'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65342"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p65code342"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// reads 5 bytes of data, starting at byte 3</span>
<span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/file_get_contents"><span style="color: #990000;">file_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</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;">NULL</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h4>file</h4>
<p>Yes, this function is simply named <a href="http://us.php.net/manual/en/function.file.php">file</a>, and it again returns the entire contents of a file. But this time, the contents are split into an array, by the newline character.</p>
<p>Let&#8217;s create a file with the following contents:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code343'); return false;">View Code</a> TXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65343"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p65code343"><pre class="txt" style="font-family:monospace;">abc
123
empty line after this
&nbsp;
last line</pre></td></tr></table></div>

<p>Now use the function:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code344'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65344"><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
</pre></td><td class="code" id="p65code344"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/file"><span style="color: #990000;">file</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contents</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* prints
&nbsp;
Array
(
    [0] =&gt; abc
&nbsp;
    [1] =&gt; 123
&nbsp;
    [2] =&gt; empty line after this
&nbsp;
    [3] =&gt;
&nbsp;
    [4] =&gt; last line
)
&nbsp;
&nbsp;
*/</span></pre></td></tr></table></div>

<p>It may not be obvious, but there is a newline character at the of each array element. If you don&#8217;t want them, you can either trim them manually, or use the FILE_IGNORE_NEW_LINES flag:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code345'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65345"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p65code345"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/file"><span style="color: #990000;">file</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> FILE_IGNORE_NEW_LINES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contents</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* prints
&nbsp;
Array
(
    [0] =&gt; abc
    [1] =&gt; 123
    [2] =&gt; empty line after this
    [3] =&gt;
    [4] =&gt; last line
)
&nbsp;
*/</span></pre></td></tr></table></div>

<p>If you do not want the empty lines either, you can use the FILE_SKIP_EMPTY_LINES flag:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code346'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65346"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p65code346"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/file"><span style="color: #990000;">file</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> FILE_SKIP_EMPTY_LINES <span style="color: #339933;">|</span> FILE_IGNORE_NEW_LINES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contents</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* prints
&nbsp;
Array
(
    [0] =&gt; abc
    [1] =&gt; 123
    [2] =&gt; empty line after this
    [3] =&gt; last line
)
&nbsp;
*/</span></pre></td></tr></table></div>

<p></p>
<h4>file_put_contents</h4>
<p>Again, there is no need to use file pointers. This function simply writes the given data to the given file:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code347'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65347"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p65code347"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0123456789&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/file_put_contents"><span style="color: #990000;">file_put_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h4>filesize, fileatime, filemtime</h4>
<p>These are alternatives to the <a href="http://us.php.net/manual/en/function.stat.php">stat</a> function for getting file information.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code348'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65348"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p65code348"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// gets the file size</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// gets the last time the file was modified</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/filemtime"><span style="color: #990000;">filemtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// gets the last time the file accessed</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/fileatime"><span style="color: #990000;">fileatime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></p>
<h2>9. File Permissions</h2>
<p>Unix-like systems have a quite detailed file permissions standard. Windows systems have it a little simpler. The whole subject of file permissions can be lengthy and a whole separate article can be written on it. So instead, we will only look at two simple concepts of file permissions: being &#8220;readable&#8221;, and being &#8220;writable&#8221;.</p>
<h4>Checking for Permissions</h4>
<p>Your script may or may not have read and/or write access to a file due to various reasons. Before attempting to open a file for reading or writing, it is wise to check if you have permission to do so:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code349'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65349"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p65code349"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_readable"><span style="color: #990000;">is_readable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// file is readable</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_writable"><span style="color: #990000;">is_writable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// file is writable</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></p>
<h4>Setting the Permissions</h4>
<p>To set the permissions of a file, we use the <a href="http://us.php.net/manual/en/function.chmod.php">chmod</a> function:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code350'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65350"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p65code350"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// makes the file read-only</span>
<a href="http://www.php.net/chmod"><span style="color: #990000;">chmod</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #208080;">0444</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// makes the file readable and writable</span>
<a href="http://www.php.net/chmod"><span style="color: #990000;">chmod</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #208080;">0666</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above code should work on both Unix and Windows systems. However, you may not be able to set the permissions, if you do not own the file or have any permissions to it in the first place.</p>
<p>To better understand chmod and file permissions in general, you can check out these links:</p>
<ul>
<li><a href="http://www.dartmouth.edu/~rc/help/faq/permissions.html">Understanding file permissions on Unix: a brief tutorial</a></li>
<li><a href="http://technet.microsoft.com/en-us/library/bb727008.aspx">File and Folder Permissions in Windows</a></li>
<li><a href="http://en.wikipedia.org/wiki/Chmod">chmod Wikipedia entry</a></li>
<li><a href="http://ss64.com/bash/chmod.html">Manual page for chmod</a></li>
</ul>
<h2>10. Using Loops for Big Files</h2>
<p>Last thing we are going to talk about is a quick tip for handling big files. This has some performance implications.</p>
<p>Easiest way to read and output a file is this:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code351'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65351"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p65code351"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/file_get_contents"><span style="color: #990000;">file_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>But that causes the entire contents of the file to be loaded into the memory. It will persist as long as it is being downloaded by the web surfer. If you have multiple people downloading files at the same time, this can consume your web servers memory very quickly.</p>
<p>A better idea for handling this would be to use a loop to read only small chunks of the file at a time. For the loop condition we will utilize the <a href="http://us.php.net/manual/en/function.feof.php">feof</a> function:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code352'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65352"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p65code352"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/feof"><span style="color: #990000;">feof</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/fread"><span style="color: #990000;">fread</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4092</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The code above will only load 4kb of data at once into the memory, and this will greatly reduce the memory usage for big file outputs. The feof function returns true when we reach the end of the file, and this breaks out of the loop.</p>
<p>Thats everything, I will be writing more soon. I will try to get another one out by Monday. Do you guys have any requests?</p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2010/12/02/tutorial-file-operations-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial: Custom PHP Templating System</title>
		<link>http://gigaspartan.com/2010/11/27/tutorial-custom-php-templating-system/</link>
		<comments>http://gigaspartan.com/2010/11/27/tutorial-custom-php-templating-system/#comments</comments>
		<pubDate>Sat, 27 Nov 2010 16:13:57 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[template system]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=27</guid>
		<description><![CDATA[Push the limits of your PHP knowledge with this advanced tutorial. Implement techniques including object-oriented programming, regular expressions, and function currying to build a templating system from scratch. Why Do I Need to Build a Templating System? The short answer? You don&#8217;t. So why bother? From the bottom of my geeky little heart, I believe [...]]]></description>
			<content:encoded><![CDATA[<p>Push the limits of your PHP knowledge with this advanced tutorial. Implement techniques including object-oriented programming, regular expressions, and function currying to build a templating system from scratch.</p>
<h2>Why Do I Need to Build a Templating System?</h2>
<p>The short answer? <em><strong>You don&#8217;t.</strong></em></p>
<p>So why bother?</p>
<p>From the bottom of my geeky little heart, I believe that all <strong>developers should constantly be pushing themselves to learn new and/or difficult concepts</strong>. It&#8217;s what makes us smarter, keeps our work interesting, and makes our day-to-day workload seem like less of a burden (because, you know, we&#8217;re getting so much smarter).</p>
<p>To that end, rolling your own templating system gives you the opportunity to hone your PHP chops with brain-bending concepts such as <a href="http://en.wikipedia.org/wiki/Currying">currying</a> and <a href="http://www.regular-expressions.info/">regular expressions</a>. And, hey! You just might find that you could put a templating system to use in one of your future projects.<br />
<span id="more-27"></span></p>
<h2>1. Plan the System</h2>
<p>Before we just dive in and start programming, let&#8217;s figure out exactly what we&#8217;re trying to do. In building this templating system, we are hoping to:</p>
<ul>
<li>Separate HTML markup from our PHP scripts almost completely</li>
<li>Make data from our back-end scripts more accessible to front-end designers</li>
<li>Simplify maintenance by abstracting display logic from business logic</li>
</ul>
<h3>What We Need to Build</h3>
<p>The templating system we&#8217;re going to build in this exercise will consist of one class, Template, containing two properties and five methods, and a folder containing the template(s) to be parsed.</p>
<p>Other than that, we just need a file for outputting some test data. Sounds pretty simple, right?</p>
<h3>How the Templating System Works</h3>
<p>To make it really easy for a designer to format the data coming out of our server-side scripts, we&#8217;re going to create a system that allows for a template that has an optional header and footer with a <strong>loop</strong> that will be used to format the entries returned from the script.</p>
<p>If we strip it down to its most basic state, this system will walk through the following steps:</p>
<ul>
<li>Load entries to be parsed as an array of objects</li>
<li>Load the template file to be used</li>
<li>Separate the header and footer from the loop</li>
<li>Find the template tags using regular expressions</li>
<li>Check if the template tag matches a property in the entry object</li>
<li>Replace the template tag with the matching property&#8217;s data</li>
</ul>
<h2>2. Outline the Class</h2>
<p>To make any programming project (or anything for that matter) easier, I like to <strong>start by outlining the steps I should take</strong>. We&#8217;ll use this tactic so that when we start developing, we&#8217;re really only filling in the blanks.</p>
<h3>Create the Folder Structure</h3>
<p>First, create a folder to contain your project. I called mine templating. Inside the project folder, create two new folders: assets and system. The assets folder will contain the templates for our demo. The system folder will contain the Template class. Inside the assets folder, create two new folders: templates. Guess what it holds!</p>
<h3>Create the Main Files</h3>
<p>Next, create index.php in the main project folder. This is where we&#8217;ll test the templating system once it&#8217;s ready to go, and we&#8217;ll use it to make sure our individual steps are working along the way as well. For now, however, you can leave it empty.</p>
<p>In the system folder, create a new PHP file called class.template.inc.php. Inside, define the class, which we&#8217;ll call Template, and let&#8217;s take our steps from above to create a to-do list:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code397'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27397"><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
</pre></td><td class="code" id="p27code397"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A templating engine
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to the MIT License, available at
 * http://www.opensource.org/licenses/mit-license.html
 *
 * @author     Pat Herlihy &lt;pat841@bu.edu&gt;
 * @copyright  2010 Boston University
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Define a class property to store the template</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Define a class property to store the entries</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a public method to output the result of the templating engine</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private method to load the template</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private method to parse the template</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>NOTE</strong>: The fact that the tag replacement method is static and that the currying function is in there at all is just something you&#8217;ll have to trust me on for now. I&#8217;ll explain everything if you stick with me.</p>
<h2>3. Define a &#8220;Template&#8221;</h2>
<p>Before we start parsing templates, let&#8217;s decide what our templates are going to look like.</p>
<p>A template should be as simple to use as possible. If we do this right, any semi-competent HTML user should be able to easily create templates. This means keeping our template as close to writing HTML as possible.</p>
<h3>What Would the HTML Look Like?</h3>
<p>To define our template, let&#8217;s start by simply mocking up an entry as we might see it on a webpage:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code398'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27398"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p27code398"><pre class="html" style="font-family:monospace;">&lt;h3&gt;Other Articles to Check Out&lt;/h3&gt;
&nbsp;
&lt;p&gt;Here are some other articles on the site.&lt;/p&gt;
&nbsp;
&lt;ul id=&quot;entries&quot;&gt;
&nbsp;
    &lt;li&gt;
        &lt;h4&gt;&lt;a href=&quot;http://gigaspartan.com/&quot;&gt;Some Article&lt;/a&gt;&lt;/h4&gt;
        &lt;p class=&quot;byline&quot;&gt;Published on gigaspartan.com&lt;/p&gt;
        &lt;p class=&quot;read-more&quot;&gt;&lt;a href=&quot;http://gigaspartan.com/&quot;&gt;read full article &amp;raquo;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
&nbsp;
&lt;/ul&gt;&lt;!-- end #entries --&gt;</pre></td></tr></table></div>

<p>Great. That&#8217;s simple enough. So let&#8217;s separate out the pieces that would vary from article to article:</p>
<ul>
<li>URL — the article URL</li>
<li>Title — the article title</li>
<li>Publisher — the site that published the article</li>
</ul>
<h3>Use the Variable Data to Choose Template Tags</h3>
<p>Using the information we gathered above, we can determine that we need a total of three template tags for the example above.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code399'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27399"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p27code399"><pre class="html" style="font-family:monospace;">&lt;h3&gt;Other Articles to Check Out&lt;/h3&gt;
&nbsp;
&lt;p&gt;Here are some other articles on the site.&lt;/p&gt;
&nbsp;
&lt;ul id=&quot;entries&quot;&gt;
&nbsp;
    &lt;li&gt;
        &lt;h4&gt;&lt;a href=&quot;url&quot;&gt;title&lt;/a&gt;&lt;/h4&gt;
        &lt;p class=&quot;byline&quot;&gt;Published on site&lt;/p&gt;
        &lt;p class=&quot;read-more&quot;&gt;&lt;a href=&quot;url&quot;&gt;read full article &amp;raquo;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
&nbsp;
&lt;/ul&gt;&lt;!-- end #entries --&gt;</pre></td></tr></table></div>

<p>But we can&#8217;t just use the word &#8220;title&#8221; as the template tag for the title; what if someone uses the word &#8220;title&#8221; in her markup? It would result in every occurrence of the word being replaced with the entry title — obviously, this isn&#8217;t the proper behavior.</p>
<p>To avoid this, we need to wrap our template tags in something that is not likely to appear in the text. For this example, we&#8217;ll use curly braces ({}).</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code400'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27400"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p27code400"><pre class="html" style="font-family:monospace;">&lt;h3&gt;Other Articles to Check Out&lt;/h3&gt;
&nbsp;
&lt;p&gt;Here are some other articles on the site.&lt;/p&gt;
&nbsp;
&lt;ul id=&quot;entries&quot;&gt;
&nbsp;
    &lt;li&gt;
        &lt;h4&gt;&lt;a href=&quot;{url}&quot;&gt;{title}&lt;/a&gt;&lt;/h4&gt;
        &lt;p class=&quot;byline&quot;&gt;Published on {site}&lt;/p&gt;
        &lt;p class=&quot;read-more&quot;&gt;&lt;a href=&quot;{url}&quot;&gt;read full article &amp;raquo;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
&nbsp;
&lt;/ul&gt;&lt;!-- end #entries --&gt;</pre></td></tr></table></div>

<p>By using a template tag that&#8217;s relatively unlikely to appear in the text of average markup, we can be relatively certain that our templates will work as expected in pretty much all standard situations.</p>
<h2>4. Load the Entries</h2>
<p>Because I&#8217;d like to get right into the internals of the templating engine, we&#8217;re not going to spend much time on the entries themselves. I&#8217;m going to use a simple array for data.</p>
<p>Open index.php in the root of your project folder and insert the following code.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code401'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27401"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p27code401"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;How to Build a Full-Featured Login System&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com/2010/11/26/how-to-build-a-full-featured-login-system/&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;site&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;posted_at&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;2010-11-26&quot;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;A Revival&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com/2010/11/26/a-revival/&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;site&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;posted_at&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;2010-11-26&quot;</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</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: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The class properties — $title, $url, $site, and $posted_at — will correspond to the template tags {title}, {url}, {site}, and {posted_at}. We&#8217;ll get to exactly how that will work a little later on.</p>
<h3>Tie the Entries to the Templating System</h3>
<p>You may have noticed that the entries aren&#8217;t being loaded or stored in the Template class right now. This is because we want our templating system to be compatible with any set of entries, and it&#8217;s pretty easy to take any set of entries from a database or web service and organize them into an array of objects.</p>
<p>However, we do need to store the entries in the templating system so they can be parsed. To keep this nice and simple, simple open up class.template.inc.php and create a public property called $entries. This will store the entries for parsing later.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code402'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27402"><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
</pre></td><td class="code" id="p27code402"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A templating engine
 * ...
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Define a class property to store the template</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the entries to be parsed in the template
     * @var array
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a public method to output the result of the templating engine</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private method to load the template</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private method to parse the template</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h2>5. Load the Template File</h2>
<p>Our next step is to create a method that will load the template file for parsing. Because this method should only run as part of the overarching templating process, we can make this method private.</p>
<p>In the interest of following <a href="http://pear.php.net/manual/en/standards.naming.php">PEAR naming conventions</a> we&#8217;ll call this method _load_template().</p>
<p>In addition, we need two new properties to store the template file path as well as the loaded template, which we&#8217;ll call $template_file and $_template, respectively.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code403'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27403"><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
</pre></td><td class="code" id="p27code403"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A templating engine
 * ...
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the location of the template file
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template_file</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the entries to be parsed in the template
     * @var array
     */</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the contents of the template file
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a public method to output the result of the templating engine</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Loads a template file with which markup should be formatted
     *
     * @return string The contents of the template file
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Load the template...</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private method to parse the template</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>NOTE</strong>: Don&#8217;t forget to adjust the $entries property to account for the addition of $template.</p>
<h3>Make Sure the File Exists</h3>
<p>Our first step in loading the template is to make sure the file exists before attempting to open it. This helps avoid errors, and really, it&#8217;s just a good idea to be sure. We&#8217;ll do this using <a href="http://php.net/file-exists">file_exists()</a>.</p>
<p>Also, because there&#8217;s the outside chance that a file&#8217;s permissions might not allow us to read its contents, we need to check that as well using <a href="http://php.net/is-readable">is_readable()</a>.</p>
<p>Because we&#8217;re trying to make this as simple as possible, we&#8217;ll be adding a default template in case the provided template doesn&#8217;t exist or doesn&#8217;t load properly for some reason.</p>
<p>After we&#8217;ve figured out the location of the template file, we can load it into our private $_template property using <a href="http://php.net/file-get-contents">file_get_contents()</a>.</p>
<p>In class.template.inc.php, add the following bold code to _load_template() to load our template (or a default):</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code404'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27404"><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
</pre></td><td class="code" id="p27code404"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A templating engine
 * ...
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the location of the template file
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the entries to be parsed in the template
     * @var array
     */</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the contents of the template file
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a public method to output the result of the templating engine</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Loads a template file with which markup should be formatted
     * ...
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Check for a custom template</span>
        <span style="color: #000088;">$template_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'assets/templates/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template_file</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$template_file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/is_readable"><span style="color: #990000;">is_readable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$template_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$template_file</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Look for a system template</span>
        <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$default_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'assets/templates/default.inc'</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/is_readable"><span style="color: #990000;">is_readable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$default_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$default_file</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// If the default template is missing, throw an error</span>
        <span style="color: #b1b100;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'No default template found'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Load the contents of the file and return them</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template <span style="color: #339933;">=</span> <a href="http://www.php.net/file_get_contents"><span style="color: #990000;">file_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private method to parse the template</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h2>6. Parse the Template</h2>
<p>To parse our template, we need to plan out a list of steps that will be performed to properly handle the data:</p>
<ul>
<li>Remove any PHP-style tags from the template</li>
<li>Extract the main entry loop from the file</li>
<li>Define a <a href="http://www.regular-expressions.info/tutorial.html">regular expressions</a> to match any template tag</li>
<li>Curry the function that will replace the tags with entry data</li>
<li>Extract the header and handle template tags if they exist</li>
<li>Extract the footer and handle template tags if they exist</li>
<li>Process each entry and insert its values into the loop</li>
<li>Return the formatted entries with the header and footer</li>
</ul>
<h3>But First, a Word About Regular Expressions</h3>
<p>With our template loaded and a plan of attack, we can start the process of parsing. This is a little trickier because we&#8217;ll be getting into regular expressions, which can be both intimidating and intoxicating for developers.</p>
<p>So before we go nuts, let&#8217;s take a second. Take a deep breath, and repeat after me: &#8220;With great power comes great responsibility. I will only use regular expressions when there isn&#8217;t a simpler way to accomplish the desired outcome. Because every time I abuse regular expressions, another programmer loses a weekend crying into his fourth Red Bull while trying to decipher the mess I&#8217;ve made.&#8221;</p>
<p>With that promise in mind, let&#8217;s dig in. Create a new private method in the Template class called _parse_template() to handle the regexes we&#8217;re about to write.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code405'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27405"><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
</pre></td><td class="code" id="p27code405"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A templating engine
 * ...
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the location of the template file
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the entries to be parsed in the template
     * @var array
     */</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the contents of the template file
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a public method to output the result of the templating engine</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Loads a template file with which markup should be formatted
     * ...
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Separates the template into header, loop, and footer for parsing
     *
     * @param array $extra      Additional content for the header/footer
     * @return string           The entry markup
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">//TODO: Remove any PHP-style comments from the template</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Extract the main entry loop from the file</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Extract the header from the template if one exists</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Extract the footer from the template if one exists</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Define a regex to match any template tag</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Process each entry and insert its values into the loop</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Curry the function that will replace the tags with entry data</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: If extra data was passed to fill in the header/footer, parse it here</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Return the formatted entries with the header and footer</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h3>Get Ready for Testing</h3>
<p>For testing purposes, we&#8217;ll need three things:</p>
<ul>
<li>A template file for testing</li>
<li>A method to return generated markup from the Template class</li>
<li>Modifications to index.php that will output the returned markup</li>
</ul>
<p>First, let&#8217;s put together a sample template that will test all of the features we&#8217;re going to build in _parse_template(). In assets/templates/, create a new file called template-test.inc and insert the following:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code406'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27406"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p27code406"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * This is a test comment
 */</span>
<span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span>Template Header<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #009900;">&#123;</span>loop<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Entry data will be processed here</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>This is content that should be displayed<span style="color: #339933;">.</span> <span style="color: #009900;">&#123;</span>test<span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span>loop<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * This is another block comment
 */</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Template footer<span style="color: #339933;">.&lt;/</span>p<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Next, we need to define our public method to generate markup, which will be called generate_markup(). This method simply calls _load_template() and _parse_template() and outputs the resulting HTML markup.</p>
<p>The generate_markup() method will eventually accept additional data that can be inserted into the header or footer of a template, so we&#8217;ll get ready for that feature as well by adding an argument to the method called $extra.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code407'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27407"><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
</pre></td><td class="code" id="p27code407"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A templating engine
 * ...
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the location of the template file
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the entries to be parsed in the template
     * @var array
     */</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the contents of the template file
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Generates markup by inserting entry data into the template file
     * 
     * @param array $extra  Extra data for the header/footer
     * @return string       The HTML with entry data inserted into the template
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_load_template<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;">$this</span><span style="color: #339933;">-&gt;</span>_parse_template<span style="color: #009900;">&#40;</span><span style="color: #000088;">$extra</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;">/**
     * Loads a template file with which markup should be formatted
     * ...
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Separates the template into header, loop, and footer for parsing
     * ...
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Finally, let&#8217;s modify index.php to output the returned value of generate_markup(). To do this, use require_once to include the Template class file, then create an instance of it.</p>
<p>With our new Template object, we can define the template file name and echo the result of generate_markup() to the browser:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code408'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27408"><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
</pre></td><td class="code" id="p27code408"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;How to Build a Full-Featured Login System&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com/2010/11/26/how-to-build-a-full-featured-login-system/&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;site&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;posted_at&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;2010-11-26&quot;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;A Revival&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com/2010/11/26/a-revival/&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;site&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;posted_at&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;2010-11-26&quot;</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Error reporting is turned up to 11 for the purposes of this demo</span>
<a href="http://www.php.net/ini_set"><span style="color: #990000;">ini_set</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display_errors&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/error_reporting"><span style="color: #990000;">ERROR_REPORTING</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Exception handling</span>
set_exception_handler<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'exception_handler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> exception_handler<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$exception</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$exception</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Load the Template class</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'system/class.template.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new instance of the Template class</span>
<span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Template<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set the testing template file location</span>
<span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'template-test.inc'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the template markup</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generate_markup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now all we have to do is temporarily output the template contents at the bottom of _parse_template() so we can see what&#8217;s happening as we parse the template:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code409'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27409"><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
</pre></td><td class="code" id="p27code409"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A templating engine
 * ...
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the location of the template file
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the entries to be parsed in the template
     * @var array
     */</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Stores the contents of the template file
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Generates markup by inserting entry data into the template file
     * ...
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Loads a template file with which markup should be formatted
     * ...
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Separates the template into header, loop, and footer for parsing
     * ...
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">//TODO: Remove any PHP-style comments from the template</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Extract the main entry loop from the file</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Extract the header from the template if one exists</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Extract the footer from the template if one exists</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Define a regex to match any template tag</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Process each entry and insert its values into the loop</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Curry the function that will replace the tags with entry data</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: If extra data was passed to fill in the header/footer, parse it here</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: Return the formatted entries with the header and footer</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// TEMPORARY: return the template after comment removal</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Make sure this is working by loading index.php in your browser. It should produce the following:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code410'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27410"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p27code410"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* * This is a test comment */</span>
Template <a href="http://www.php.net/header"><span style="color: #990000;">Header</span></a>
<span style="color: #009900;">&#123;</span>loop<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Entry data will be processed here</span>
&nbsp;
This is content that should be displayed<span style="color: #339933;">.</span> <span style="color: #009900;">&#123;</span>test<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span>loop<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">/* * This is another block comment */</span>
&nbsp;
Template footer<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p></p>
<h3>Remove Comments from the Template File</h3>
<p>Our first regexes will remove any PHP-style comments from a template file. While this isn&#8217;t a strictly necessary step, keep in mind that we&#8217;re trying to keep this system as user-friendly as possible. It would be extremely helpful to a potential template builder to know what tags are available, but it&#8217;s probably not desirable to have those comments in your final markup (not to mention that a PHP-style comment will break HTML layouts).</p>
<p>The two comment styles we&#8217;re going to approach are the recommended comment styles in PHP:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code411'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27411"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p27code411"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * This is a block-level comment
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// This is a one-line comment</span></pre></td></tr></table></div>

<p>First, let&#8217;s focus on the regex that will capture block-level comments. This needs to match any string starting with /* and ending with */. Right out of the gate, our regex will look like this:</p>
<p>$pattern = &#8216;#/\*\*/#&#8217;;<br />
Because the standard regex delimiter is the forward slash (/), we&#8217;re going to use an alternative delimiter to reduce the number of escaped characters required in our regex. The number sign (#) is a perfectly valid regex delimiter — though I usually recommend using the standard forward slash for clarity, in certain cases using the standard can decrease the readability of a regular expression. Compare the following:</p>
<p>$pattern = &#8216;#/\*\*/#&#8217;; // Valid and easy to read</p>
<p>$pattern = &#8216;/\/\*\*\//&#8217;; // Identical in function, but a little harder to read<br />
If we insert this regex into _parse_template() as is, though, it doesn&#8217;t yield the desired result. Add the bold code below to _parse_template() to see the result of our current regex:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code412'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27412"><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
</pre></td><td class="code" id="p27code412"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">// Create an alias of the template file property to save space</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Remove any PHP-style comments from the template</span>
        <span style="color: #000088;">$comment_pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'#/\*\*/#'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_pattern</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: To-do items snipped for brevity...</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// TEMPORARY: return the template after comment removal</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$template</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>NOTE</strong>: For the remainder of this article, docblock comments will be omitted to save space (unless they&#8217;re new).</p>
<p>The output in your browser doesn&#8217;t change if you reload index.php; this happens because we haven&#8217;t added the wildcard character with a modifier to account for zero or more characters between the comment opening and closing (.*).</p>
<p>Additionally, we need to account for the fact that block-level comments are usually multi-line, we need to add the s modifier to account for this.</p>
<p>Our modified regex should look like this:</p>
<p>$comment_pattern = &#8216;#/\*.*\*/#s&#8217;;<br />
Adjust this in _parse_template(), then reload index.php in your browser.</p>
<p>Whoops! The output is:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code413'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27413"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p27code413"><pre class="html" style="font-family:monospace;">Template footer.</pre></td></tr></table></div>

<p>We forgot to make the wildcard character lazy, so instead of stopping at the end of the first comment, it continued on to the end of the second block comment. This is easy to fix, though: simply add a question mark after the wildcard to make it lazy. It should look like this:</p>
<p>$comment_pattern = &#8216;#/\*.*?\*/#s&#8217;;<br />
Adjust _parse_template(), then reload index.php. Much better!</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code414'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27414"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p27code414"><pre class="php" style="font-family:monospace;">Template <a href="http://www.php.net/header"><span style="color: #990000;">Header</span></a>
<span style="color: #009900;">&#123;</span>loop<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Entry data will be processed here</span>
&nbsp;
This is content that should be displayed<span style="color: #339933;">.</span> <span style="color: #009900;">&#123;</span>test<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span>loop<span style="color: #009900;">&#125;</span>
&nbsp;
Template footer<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>Next, we need to target inline comments (those starting with two forward slashes (//). Because these aren&#8217;t multi-line, this will actually be its own regex instead of expanding the block-level comment regex.</p>
<p>For this regex, we need to find any text following two forward slashes (//). The only exception to this rule is the two forward slashes in a remote URL (http://) — to exclude this, we&#8217;ll use a <a href="http://www.regular-expressions.info/lookaround.html#lookbehind">negative lookbehind</a>.</p>
<p>The finished regular expression should look like this:</p>
<p>#(?<!:)//.*#<br />
Add this to _parse_template() by changing the variable $comment_pattern to an array with our block-level regex as the first item and the inline comment regex as the second:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code415'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27415"><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
</pre></td><td class="code" id="p27code415"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">// Create an alias of the template file property to save space</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Remove any PHP-style comments from the template</span>
        <span style="color: #000088;">$comment_pattern</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#/\*.*?\*/#s'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#(?&lt;!:)//.*#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_pattern</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: To-do items snipped for brevity...</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// TEMPORARY: return the template after comment removal</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$template</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now the comments are properly stripped when you reload index.php in your browser:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code416'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27416"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p27code416"><pre class="html" style="font-family:monospace;">Template Header  
{loop}  
&nbsp;
This is content that should be displayed. {test}  
{/loop}  
&nbsp;
Template footer.</pre></td></tr></table></div>

<p></p>
<h3>Separate the Header, Footer, and Loop for Processing</h3>
<p>Our next task is to split the template into three sections: the header, the loop, and the footer. These won&#8217;t necessarily exist in all cases, so we&#8217;ll have to check for that as well.</p>
<h3>Isolate the Main Entry Loop</h3>
<p>First, we&#8217;ll grab the loop by catching all content between the {loop} and {/loop} template tags. This regex will match the whole template and use a capturing group to identify the loop:</p>
<p>#.*{loop}(.*?){/loop}.*#is<br />
Modify _parse_template() as shown in bold to test that the entry loop is being extracted properly:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code417'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27417"><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
</pre></td><td class="code" id="p27code417"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">// Create an alias of the template file property to save space</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Remove any PHP-style comments from the template</span>
        <span style="color: #000088;">$comment_pattern</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#/\*.*?\*/#s'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#(?&lt;!:)//.*#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_pattern</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the main entry loop from the file</span>
        <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'#.*{loop}(.*?){/loop}.*#is'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$entry_template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: To-do items snipped for brevity...</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// TEMPORARY: return the loop after isolating it</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$entry_template</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>NOTE</strong>: Don&#8217;t forget to alter the function to return $entry_template so you can see the proper output.</p>
<p>By using preg_replace() to &#8220;replace&#8221; the whole template with just the captured loop template, we&#8217;ve successfully isolated the main loop. Reload index.php in your browser and you should see the following:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code418'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27418"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p27code418"><pre class="html" style="font-family:monospace;">This is content that should be displayed. {test}</pre></td></tr></table></div>

<p></p>
<h3>Isolate the Header</h3>
<p>Next, let&#8217;s get the header out of the template. The regex to do this will be similar to the one that grabbed the main loop, but this time we&#8217;re going to capture the content before the {loop} template tag.</p>
<p>To match the header, we need to start from the beginning of the template and capture everything up until the {loop} tag. Since we&#8217;re using preg_replace() to extract the header, we also need to match everything after the {loop} tag as well to make sure it gets removed when the replacement occurs.</p>
<p>This regex, when it&#8217;s completed, should look like this:</p>
<p>/^(.*)?{loop.*$/is<br />
Because some templates won&#8217;t require a header, we also need to check that the data returned in header isn&#8217;t the whole template. If that happens, the header should be set to NULL to avoid duplicated data.</p>
<p>Modify _parse_template() with the bold code and set it to return the extracted header:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code419'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27419"><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
</pre></td><td class="code" id="p27code419"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">// Create an alias of the template file property to save space</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Remove any PHP-style comments from the template</span>
        <span style="color: #000088;">$comment_pattern</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#/\*.*?\*/#s'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#(?&lt;!:)//.*#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_pattern</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the main entry loop from the file</span>
        <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'#.*{loop}(.*?){/loop}.*#is'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$entry_template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the header from the template if one exists</span>
        <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(.*)?{loop.*$/is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$header</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: To-do items snipped for brevity...</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// TEMPORARY: return the header after isolating it</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$header</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>As expected, reloading index.php in your browser will result in the header data being displayed:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code420'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27420"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p27code420"><pre class="html" style="font-family:monospace;">Template Header</pre></td></tr></table></div>

<p></p>
<h3>Isolate the Footer</h3>
<p>The footer is very similar to the header in how it&#8217;s extracted, except this time we&#8217;re capturing the data after the {/loop} tag and making sure it doesn&#8217;t match the whole template using this regex:</p>
<p>#^.*?{/loop}(.*)$#is<br />
Plug this into _parse_template() and set the method to return the footer content just like we did with the header using the bold code:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code421'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27421"><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
</pre></td><td class="code" id="p27code421"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">// Create an alias of the template file property to save space</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Remove any PHP-style comments from the template</span>
        <span style="color: #000088;">$comment_pattern</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#/\*.*?\*/#s'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#(?&lt;!:)//.*#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_pattern</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the main entry loop from the file</span>
        <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'#.*{loop}(.*?){/loop}.*#is'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$entry_template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the header from the template if one exists</span>
        <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(.*)?{loop.*$/is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$header</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the footer from the template if one exists</span>
        <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#^.*?{/loop}(.*)$#is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$footer</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: To-do items snipped for brevity...</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// TEMPORARY: return the footer after isolating it</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$footer</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Reload the index file to see the footer output:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code422'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27422"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p27code422"><pre class="html" style="font-family:monospace;">Template footer.</pre></td></tr></table></div>

<p></p>
<h3>Identify Template Tags with Regular Expressions</h3>
<p>The next step in our process is to put together a regex that will match any template tag so that we can replace them with entry data.</p>
<p>Unlike the others we&#8217;ve written, this one shouldn&#8217;t match the whole template. This pattern should match <strong>only the template tag that will be replaced</strong>.</p>
<p>To accomplish this, we can use the shorthand to match any word character (\w is equivalent to [A-Za-z0-9_]) and match one or more characters between curly braces. The complete regex looks like this:</p>
<p>/{(\w+)}/<br />
We&#8217;ll verify that this works in just a bit, but for now let&#8217;s just define the pattern for later by adding the bold code to _parse_template():</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code423'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27423"><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
</pre></td><td class="code" id="p27code423"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">// Create an alias of the template file property to save space</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Remove any PHP-style comments from the template</span>
        <span style="color: #000088;">$comment_pattern</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#/\*.*?\*/#s'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#(?&lt;!:)//.*#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_pattern</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the main entry loop from the file</span>
        <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'#.*{loop}(.*?){/loop}.*#is'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$entry_template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the header from the template if one exists</span>
        <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(.*)?{loop.*$/is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$header</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the footer from the template if one exists</span>
        <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#^.*?{/loop}(.*)$#is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$footer</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Define a regex to match any template tag</span>
        <span style="color: #000088;">$tag_pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/{(\w+)}/'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: To-do items snipped for brevity...</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a private currying function to facilitate tag replacement</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>With all of our regular expressions ready to rock, we can move on to the next big task.</p>
<h2>7. Get Ready for Template Tag Replacement: Currying</h2>
<p>In order to replace our template tags with the properties from our entries, we need to use a concept called <a href="http://en.wikipedia.org/wiki/Currying">currying</a>, which is the act of making a function that takes multiple arguments into a chain of functions that accept a single argument.</p>
<h3>Wait. What Did You Just Say?</h3>
<p>Currying is kind of a weird concept to get your head around, so let&#8217;s take a second to go over exactly how it works.</p>
<p>The goal of a currying function is to make it possible to supply one argument at a time to a function that requires multiple aruguments without causing errors. So, for instance, let&#8217;s look at a simple math function:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code424'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27424"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p27code424"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> add<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: #b1b100;">return</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;">&#125;</span></pre></td></tr></table></div>

<p>If we wanted to call this function normally, we&#8217;d simply do the following:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code425'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27425"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p27code425"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> add<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Output: 3</span></pre></td></tr></table></div>

<p>But let&#8217;s say that — for whatever reason — we needed to call the add() function incrementally; we can&#8217;t simply add one argument now and another later (by calling add(1)). That issues a warning:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code426'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27426"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p27code426"><pre class="php" style="font-family:monospace;">Warning<span style="color: #339933;">:</span> Missing argument <span style="color: #cc66cc;">2</span> <span style="color: #b1b100;">for</span> add<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">...</span>
&nbsp;
Notice<span style="color: #339933;">:</span> Undefined variable<span style="color: #339933;">:</span> y in <span style="color: #339933;">...</span></pre></td></tr></table></div>

<p>So we need an intermediate step that will check if the proper number of arguments were passed. If so, the function executes as usual. However, if there are too few arguments, a new function will be returned with the function name and the first argument stored. This way, when the second argument is passed the original function can be called properly.</p>
<p>Using our add() function as an example and assuming that we can curry this function using the imaginary function curry(), we can demonstrate this process:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code427'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27427"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p27code427"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * Start by currying the function - argument 1 is the function name,
 * argument 2 is the number of arguments expected
 */</span>
<span style="color: #000088;">$curried</span> <span style="color: #339933;">=</span> curry<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'add'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Returns the curried add function</span>
&nbsp;
<span style="color: #000088;">$partial</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$curried</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Returns a function with the argument '1' stored</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$partial</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Output: 3</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Try passing the expected number of arguments to the curried function</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$curried</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Output: 4</span></pre></td></tr></table></div>

<p></p>
<h3>Write the Currying Method</h3>
<p>Now that we know how currying works, let&#8217;s start writing the function.</p>
<p>The currying function itself will always return a function, which we&#8217;ll do using <a href="http://php.net/create-function">create_function()</a>.</p>
<p>The created function will check if the proper number of arguments exist and execute the curried function as usual if so using <a href="http://php.net/call-user-func-array">call_user_func_array()</a>. If there aren&#8217;t enough arguments, another function will be returned using create_function().</p>
<p>Because putting all of this together requires creating functions within created functions, there&#8217;s a lot of escaping. This makes our currying method look more confusing than it really is. Add it to the Template class with the following bold code:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code428'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27428"><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
</pre></td><td class="code" id="p27code428"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//TODO: Write a static method to replace the template tags with entry data</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * A currying function
     *
     * Currying allows a function to be called incrementally. This means that if
     * a function accepts two arguments, it can be curried with only one
     * argument supplied, which returns a new function that will accept the
     * remaining argument and return the output of the original curried function
     * using the two supplied parameters.
     *
     * Example:
&nbsp;
        function add($a, $b)
        {
         return $a + $b;
        }
&nbsp;
        $func = $this-&gt;_curry('add', 2);
&nbsp;
        $func2 = $func(1); // Stores 1 as the first argument of add()
&nbsp;
        echo $func2(2); // Executes add() with 2 as the second arg and outputs 3
&nbsp;
     * @param string $function  The name of the function to curry
     * @param int $num_args     The number of arguments the function accepts
     * @return mixed            Function or return of the curried function
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _curry<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$num_args</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <a href="http://www.php.net/create_function"><span style="color: #990000;">create_function</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;
            // Store the passed arguments in an array
            <span style="color: #000099; font-weight: bold;">\$</span>args = func_get_args();
&nbsp;
            // Execute the function if the right number of arguments were passed
            if( count(<span style="color: #000099; font-weight: bold;">\$</span>args)&gt;=<span style="color: #006699; font-weight: bold;">$num_args</span> )
            {
                return call_user_func_array('<span style="color: #006699; font-weight: bold;">$function</span>', <span style="color: #000099; font-weight: bold;">\$</span>args);
            }
&nbsp;
            // Export the function arguments as executable PHP code
            <span style="color: #000099; font-weight: bold;">\$</span>args = var_export(<span style="color: #000099; font-weight: bold;">\$</span>args, 1);
&nbsp;
            // Return a new function with the arguments stored otherwise
            return create_function('','
                <span style="color: #000099; font-weight: bold;">\$</span>a = func_get_args();
                <span style="color: #000099; font-weight: bold;">\$</span>z = ' . <span style="color: #000099; font-weight: bold;">\$</span>args . ';
                <span style="color: #000099; font-weight: bold;">\$</span>a = array_merge(<span style="color: #000099; font-weight: bold;">\$</span>z,<span style="color: #000099; font-weight: bold;">\$</span>a);
                return call_user_func_array(\'<span style="color: #006699; font-weight: bold;">$function</span>\', <span style="color: #000099; font-weight: bold;">\$</span>a);
            ');
        &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h3>How Does Currying Apply to the Templating System?</h3>
<p>Right now it might not be clear how all of this applies to the templating system. When we get to the next step we&#8217;ll go over this in detail, but in short, we need to be able to call a function with two arguments in order to replace the template tags: one argument is the entry from which data should be pulled, and the other is the template tag to be replaced.</p>
<p>Since we&#8217;re using regular expressions to replace the tags, we need to use <a href="http://php.net/preg-replace-callback">preg_replace_callback()</a> to make the replacements. However, since the callback passed to that function can only accept one argument — the matched text — we need to pass a curried function that already has the entry stored inside of it.</p>
<p>Make sense? Let&#8217;s make it happen!</p>
<h2>8. Replace Template Tags with Matching Entry Data</h2>
<p>All the pieces are in place. Now we just need to connect the dots and get it done.</p>
<h3>Write the Tag Replacing Method</h3>
<p>Replacing tags is fairly simple on its own: take the matched text from the template tag and see if a property exists in the entry object by that name. If so, return the data stored in said property; if not, just return the template tag so the designer can see that something went wrong (or, in edge cases, the odd string that was wrapped in curly braces doesn&#8217;t break).</p>
<p>This function is going to be called replace_tags(), and it will be static so that it can be passed to the currying function as a valid callback. Add it to the Template class using the bold code below:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code429'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27429"><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
</pre></td><td class="code" id="p27code429"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Replaces template tags with the corresponding entry data
     *
     * @param string $entry     A serialized entry object
     * @param array $params     Parameters for replacement
     * @param array $matches    The match array from preg_replace_callback()
     * @return string           The replaced template value
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> replace_tags<span style="color: #009900;">&#40;</span><span style="color: #000088;">$entry</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Unserialize the object</span>
        <span style="color: #000088;">$entry</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/unserialize"><span style="color: #990000;">unserialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$entry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Make sure the template tag has a matching array element</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> property_exists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$entry</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</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: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Grab the value from the Entry object</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$entry</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Otherwise, simply return the tag as is</span>
        <span style="color: #b1b100;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;{&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$matches</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: #0000ff;">&quot;}&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _curry<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$num_args</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>NOTE</strong>: The call to <a href="http://php.net/unserialize">unserialize()</a> at the top of this method is due to an issue with passing an object through a currying function. We&#8217;ll serialize the object in the next step.</p>
<h3>Modify the Template Parsing Method to Replace Template Tags</h3>
<p>To complete our templating system, we first need to get our curried callback function ready for use with all of the replacement calls. This is done by currying Template::replace_tags() and storing it in a variable called $callback. Add this to _parse_template() with the bold code below:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code430'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27430"><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
</pre></td><td class="code" id="p27code430"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">// Create an alias of the template file property to save space</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Remove any PHP-style comments from the template</span>
        <span style="color: #000088;">$comment_pattern</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#/\*.*?\*/#s'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#(?&lt;!:)//.*#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_pattern</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the main entry loop from the file</span>
        <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'#.*{loop}(.*?){/loop}.*#is'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$entry_template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the header from the template if one exists</span>
        <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(.*)?{loop.*$/is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$header</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the footer from the template if one exists</span>
        <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#^.*?{/loop}(.*)$#is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$footer</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Define a regex to match any template tag</span>
        <span style="color: #000088;">$tag_pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/{(\w+)}/'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Curry the function that will replace the tags with entry data</span>
        <span style="color: #000088;">$callback</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_curry<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Template::replace_tags'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TODO: To-do items snipped for brevity...</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> replace_tags<span style="color: #009900;">&#40;</span><span style="color: #000088;">$entry</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _curry<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$num_args</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Next, we need to set up a loop to go through each entry object in the $entries array. With each entry, we need to call preg_replace_callback() with the template tag regex as the first argument, the callback with the serialized entry object as the second argument, and the loop as the third argument.</p>
<p>The markup returned from each call should be appended to a variable called $markup, which stores all of the entry markup to be output to the browser.</p>
<p>Add this to _parse_template() with the bold code:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code431'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27431"><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
</pre></td><td class="code" id="p27code431"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">// Create an alias of the template file property to save space</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Remove any PHP-style comments from the template</span>
        <span style="color: #000088;">$comment_pattern</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#/\*.*?\*/#s'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#(?&lt;!:)//.*#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_pattern</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the main entry loop from the file</span>
        <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'#.*{loop}(.*?){/loop}.*#is'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$entry_template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the header from the template if one exists</span>
        <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(.*)?{loop.*$/is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$header</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the footer from the template if one exists</span>
        <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#^.*?{/loop}(.*)$#is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$footer</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Define a regex to match any template tag</span>
        <span style="color: #000088;">$tag_pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/{(\w+)}/'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Curry the function that will replace the tags with entry data</span>
        <span style="color: #000088;">$callback</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_curry<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Template::replace_tags'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Process each entry and insert its values into the loop</span>
        <span style="color: #000088;">$markup</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</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;">$c</span><span style="color: #339933;">=</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entries</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;">$c</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;">$markup</span> <span style="color: #339933;">.=</span> <a href="http://www.php.net/preg_replace_callback"><span style="color: #990000;">preg_replace_callback</span></a><span style="color: #009900;">&#40;</span>
                            <span style="color: #000088;">$tag_pattern</span><span style="color: #339933;">,</span>
                            <span style="color: #000088;">$callback</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/serialize"><span style="color: #990000;">serialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entries</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: #339933;">,</span>
                            <span style="color: #000088;">$entry_template</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//TEMPORARY: Output the markup after replacing template tags</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$markup</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> replace_tags<span style="color: #009900;">&#40;</span><span style="color: #000088;">$entry</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _curry<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$num_args</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Before we can test this, we need to create an entry so that the engine has something to loop through. In index.php, add a dummy entry with a property called $test to match the template tag in our testing template file:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code432'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27432"><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
</pre></td><td class="code" id="p27code432"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;How to Build a Full-Featured Login System&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com/2010/11/26/how-to-build-a-full-featured-login-system/&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;site&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;posted_at&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;2010-11-26&quot;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;A Revival&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com/2010/11/26/a-revival/&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;site&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;posted_at&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;2010-11-26&quot;</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Error reporting is turned up to 11 for the purposes of this demo</span>
<a href="http://www.php.net/ini_set"><span style="color: #990000;">ini_set</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display_errors&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/error_reporting"><span style="color: #990000;">ERROR_REPORTING</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Exception handling</span>
set_exception_handler<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'exception_handler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> exception_handler<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$exception</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$exception</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Load the Template class</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'system/class.template.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new instance of the Template class</span>
<span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Template<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set the testing template file location</span>
<span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'template-test.inc'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entries</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'test'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'This was inserted using template tags!'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the template markup</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generate_markup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>With a dummy entry present, we can test the template tag replacement by reloading index.php in our browser. The output should read:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code433'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27433"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p27code433"><pre class="html" style="font-family:monospace;">This is content that should be displayed. This was inserted using template tags!</pre></td></tr></table></div>

<p>This means that we&#8217;ve effectively created a templating engine! However, we&#8217;re not quite done yet: we still need to add the ability to replace template tags in the header and footer of our template file.</p>
<h3>Replace Template Tags in the Header and Footer of the Template</h3>
<p>The process for replacing header and footer template tags is identical to the process for those in the loop, but different data is necessary in order to do so.</p>
<p>You may remember that we included an argument called $extra when we were writing the generate_markup() and _parse_template() methods; this variable is to be used as an object that will store data for replacing the header and footer template tags. For our purposes, $extra can contain two properties, $header and $footer, both of which will store an object whose properties will be used to replace template tags in the corresponding section of the template.</p>
<p>Obviously, if no header or footer tags exist, then no data will be stored in $extra for processing. For that reason, we start by checking if $extra is an object. If so, we&#8217;ll loop through its properties and run preg_replace_callback() with the template tag regex, the callback after being passed the serialized header or footer object, and the header or footer section of the template.</p>
<p>Add the bold code below to complete our templating system:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code434'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27434"><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
</pre></td><td class="code" id="p27code434"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Template
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$template</span><span style="color: #339933;">,</span>
           <span style="color: #000088;">$entries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_template</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generate_markup<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _load_template<span style="color: #009900;">&#40;</span>  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _parse_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</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: #666666; font-style: italic;">// Create an alias of the template file property to save space</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_template<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Remove any PHP-style comments from the template</span>
        <span style="color: #000088;">$comment_pattern</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#/\*.*?\*/#s'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#(?&lt;!:)//.*#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_pattern</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the main entry loop from the file</span>
        <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'#.*{loop}(.*?){/loop}.*#is'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$entry_template</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the header from the template if one exists</span>
        <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(.*)?{loop.*$/is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$header</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Extract the footer from the template if one exists</span>
        <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#^.*?{/loop}(.*)$#is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$footer</span><span style="color: #339933;">===</span><span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Define a regex to match any template tag</span>
        <span style="color: #000088;">$tag_pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/{(\w+)}/'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Curry the function that will replace the tags with entry data</span>
        <span style="color: #000088;">$callback</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_curry<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Template::replace_tags'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Process each entry and insert its values into the loop</span>
        <span style="color: #000088;">$markup</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</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;">$c</span><span style="color: #339933;">=</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entries</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;">$c</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;">$markup</span> <span style="color: #339933;">.=</span> <a href="http://www.php.net/preg_replace_callback"><span style="color: #990000;">preg_replace_callback</span></a><span style="color: #009900;">&#40;</span>
                            <span style="color: #000088;">$tag_pattern</span><span style="color: #339933;">,</span>
                            <span style="color: #000088;">$callback</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/serialize"><span style="color: #990000;">serialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entries</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: #339933;">,</span>
                            <span style="color: #000088;">$entry_template</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// If extra data was passed to fill in the header/footer, parse it here</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <a href="http://www.php.net/is_object"><span style="color: #990000;">is_object</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$extra</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$extra</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$props</span> <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$$key</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace_callback"><span style="color: #990000;">preg_replace_callback</span></a><span style="color: #009900;">&#40;</span>
                            <span style="color: #000088;">$tag_pattern</span><span style="color: #339933;">,</span>
                            <span style="color: #000088;">$callback</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/serialize"><span style="color: #990000;">serialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$extra</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                            <span style="color: #000088;">$$key</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Return the formatted entries with the header and footer reattached</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$header</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$markup</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$footer</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> replace_tags<span style="color: #009900;">&#40;</span><span style="color: #000088;">$entry</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _curry<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$num_args</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you reload index.php in your browser, you&#8217;ll see the following output:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code435'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27435"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p27code435"><pre class="html" style="font-family:monospace;">Template Header  
&nbsp;
This is content that should be displayed. This was inserted using template tags!  
&nbsp;
Template footer.</pre></td></tr></table></div>

<p>The last thing to do is to test the header and footer template tag replacement. Open template-test.inc and add two new template tags, one in the header and one in the footer:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code436'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27436"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p27code436"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * This is a test comment
 */</span>
<span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span>Template Header<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span>header_stuff<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#123;</span>loop<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Entry data will be processed here</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>This is content that should be displayed<span style="color: #339933;">.</span> <span style="color: #009900;">&#123;</span>test<span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span>loop<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * This is another block comment
 */</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Template footer<span style="color: #339933;">.</span> <span style="color: #009900;">&#123;</span>footerStuff<span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Next, go back to index.php and add a new object called $extra with two objects stored in its $header and $footer properties that have properties corresponding to the new template tags:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code437'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27437"><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
</pre></td><td class="code" id="p27code437"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;How to Build a Full-Featured Login System&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com/2010/11/26/how-to-build-a-full-featured-login-system/&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;site&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;posted_at&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;2010-11-26&quot;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;A Revival&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com/2010/11/26/a-revival/&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;site&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;posted_at&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;2010-11-26&quot;</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Error reporting is turned up to 11 for the purposes of this demo</span>
<a href="http://www.php.net/ini_set"><span style="color: #990000;">ini_set</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display_errors&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/error_reporting"><span style="color: #990000;">ERROR_REPORTING</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Exception handling</span>
set_exception_handler<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'exception_handler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> exception_handler<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$exception</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$exception</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Load the Template class</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'system/class.template.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new instance of the Template class</span>
<span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Template<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set the testing template file location</span>
<span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'template-test.inc'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entries</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'test'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'This was inserted using template tags!'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$extra</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'header'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
                        <span style="color: #0000ff;">'header_stuff'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Some extra content.'</span>
                    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'footer'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
                        <span style="color: #0000ff;">'footerStuff'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'More extra content.'</span>
                    <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the template markup</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generate_markup</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$extra</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>NOTE</strong>: Don&#8217;t forget to pass $extra to generate_markup()!</p>
<p>Save these changes, reload the file in your browser, and you&#8217;ll see the following:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code438'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27438"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p27code438"><pre class="html" style="font-family:monospace;">Template Header  
Some extra content.  
&nbsp;
This is content that should be displayed. This was inserted using template tags!  
&nbsp;
Template footer. More extra content.</pre></td></tr></table></div>

<p></p>
<h2>9. Use Real Entries</h2>
<p>As a final exercise, let&#8217;s use some real entries from the array and design a template to display them.</p>
<h3>Create a Template</h3>
<p>For a template, let&#8217;s create a new on in the templates folder called entry-list.inc. Inside, add the following code:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code439'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27439"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p27code439"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * This template has the following tags available:
 *      title       Article title
 *      url         Permalink of the article
 *      site        Site on which the article was originally published
 *      posted_at   Original posting date
 */</span>
&nbsp;
<span style="color: #339933;">&lt;</span>h3<span style="color: #339933;">&gt;</span>Entry Short List<span style="color: #339933;">&lt;/</span>h3<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>ol id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;entry-list&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// This is the main entry loop</span>
<span style="color: #009900;">&#123;</span>loop<span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{url}&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span>title<span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#40;</span>published on <span style="color: #009900;">&#123;</span>site<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span>loop<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>ol<span style="color: #339933;">&gt;&lt;!--</span> <a href="http://www.php.net/end"><span style="color: #990000;">end</span></a> <span style="color: #666666; font-style: italic;">#entry-list --&gt;</span></pre></td></tr></table></div>

<p></p>
<h3>Load Real Entries and Use the New Template</h3>
<p>In index.php, alter the code to use the new template file and to load the latest entries from the array:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p27code440'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27440"><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
</pre></td><td class="code" id="p27code440"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Error reporting is turned up to 11 for the purposes of this demo</span>
<a href="http://www.php.net/ini_set"><span style="color: #990000;">ini_set</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display_errors&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/error_reporting"><span style="color: #990000;">ERROR_REPORTING</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;How to Build a Full-Featured Login System&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com/2010/11/26/how-to-build-a-full-featured-login-system/&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;site&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;posted_at&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;2010-11-26&quot;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;A Revival&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com/2010/11/26/a-revival/&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;site&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://gigaspartan.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;posted_at&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;2010-11-26&quot;</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Exception handling</span>
set_exception_handler<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'exception_handler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> exception_handler<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$exception</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$exception</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html;charset=utf-8&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;!--</span> Meta information <span style="color: #339933;">--&gt;</span>
    <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Demo<span style="color: #339933;">:</span> Roll Your Own Templating <a href="http://www.php.net/system"><span style="color: #990000;">System</span></a> in PHP<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>meta name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;description&quot;</span>
          content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;A demo of the templating system by Jason Lengstorf&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Load the Template class</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'system/class.template.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new instance of the Template class</span>
<span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Template<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set the testing template file location</span>
<span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'entry-list.inc'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Load sample entries</span>
<span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entries</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$array</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the template markup</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generate_markup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>NOTE</strong>: I added in a doctype declaration and basic HTML tags to avoid character encoding issues.</p>
<h2>Summary</h2>
<p>At this point, you&#8217;ve successfully combined object-oriented PHP, regular expressions, and function currying into an easy-to-use templating system. The techniques in this tutorial can be added to your development arsenal for use in future projects, and hopefully you&#8217;re feeling like a better developer right about now.</p>
<p>Did you spot a shortcut I missed? Can you think of a way to improve the templating system? Let me know your thoughts in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2010/11/27/tutorial-custom-php-templating-system/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

