<?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>ElbertF &#187; programming</title>
	<atom:link href="http://elbertf.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://elbertf.com</link>
	<description>Insights and Updates from a Tech Geek</description>
	<lastBuildDate>Thu, 02 Feb 2012 01:22:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Swiftlet 1.0.0 Stable</title>
		<link>http://elbertf.com/2009/05/swiftlet-100-stable/</link>
		<comments>http://elbertf.com/2009/05/swiftlet-100-stable/#comments</comments>
		<pubDate>Fri, 08 May 2009 02:51:32 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[app updates]]></category>
		<category><![CDATA[gpl]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[pintail]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[swiftlet]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=164</guid>
		<description><![CDATA[Swiftlet, the light-weight PHP framework that I&#8217;ve been working on for a while, is now&#160;stable. Feature-wise not much has changed since the Beta and Release Candidate cycles but the code has been thoroughly tested and improved where possible. If you&#8217;re planning on building a PHP website, give Swiftlet a&#160;try. I moved the project page and [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://code.google.com/p/swiftlet/logo?logo_id=1240554537" alt="Swiftlet logo" class="left"/><a href="http://swiftlet.org/">Swiftlet</a>, the light-weight PHP framework that I&#8217;ve been working on for a while, is now&nbsp;stable.</p>
<p>Feature-wise not much has changed since the Beta and Release Candidate cycles but the code has been thoroughly tested and improved where possible. If you&#8217;re planning on building a PHP website, <a href="http://code.google.com/p/swiftlet/downloads/list">give Swiftlet a&nbsp;try</a>.</p>
<p>I moved the project page and documentation away from Google Code, if you go to <a href="http://swiftlet.org/">swiftlet.org</a> you&#8217;ll find the new page. It&#8217;s powered by a documentation system that I custom coded (dubbed Pintail). If there is any interest I will release the code behind it as Open-Source as&nbsp;well.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/05/swiftlet-100-stable/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Swiftlet Beta</title>
		<link>http://elbertf.com/2009/05/swiftlet-beta/</link>
		<comments>http://elbertf.com/2009/05/swiftlet-beta/#comments</comments>
		<pubDate>Sat, 02 May 2009 08:17:52 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[app updates]]></category>
		<category><![CDATA[gpl]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[swiftlet]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=156</guid>
		<description><![CDATA[Swiftlet is now in beta, after 15 alpha cycles I&#8217;m confident that it&#8217;s now pretty much feature complete (there core that is, there will be plenty more plug-ins) and relatively&#160;stable. One of the most important recently added features is the plug-in installer. It checks for compatibility with the core code and creates and populates database [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://code.google.com/p/swiftlet/logo?logo_id=1240554537" alt="Swiftlet logo" class="left"/><a href="http://swiftlet.org">Swiftlet</a> is now in beta, after 15 alpha cycles I&#8217;m confident that it&#8217;s now pretty much feature complete (there core that is, there will be plenty more plug-ins) and relatively&nbsp;stable.</p>
<p>One of the most important recently added features is the plug-in installer. It checks for compatibility with the core code and creates and populates database tables with a click of the mouse. Plug-ins that don&#8217;t require a database connection don&#8217;t need to be installed; they&#8217;re <em>plug-and-play</em> (and Swiftlet runs fine without a&nbsp;database).</p>
<p>I also added plug-ins to handle user sessions and authorization. This should make it easy to create a website that requires a login&nbsp;system.</p>
<p>Download:&nbsp;<a href="http://code.google.com/p/swiftlet/downloads/list">code.google.com/p/swiftlet/downloads/list</a></p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/05/swiftlet-beta/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Swiftlet — light-weight PHP framework</title>
		<link>http://elbertf.com/2009/04/swiftlet-%e2%80%94-light-weight-php-framework/</link>
		<comments>http://elbertf.com/2009/04/swiftlet-%e2%80%94-light-weight-php-framework/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 03:48:55 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[app updates]]></category>
		<category><![CDATA[gpl]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[swiftlet]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=132</guid>
		<description><![CDATA[I just released an early alpha version of Swiftlet, an Open Source, light-weight PHP framework released under the GPL&#160;license. It&#8217;s targeted at developers who want to built simple websites that don&#8217;t require large and complex frameworks, but do want a solid base to work from. Swiftlet provides basic security features such as user input sanitizing, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://code.google.com/p/swiftlet/logo?logo_id=1240554537" alt="Swiftlet logo" class="left"/>I just released an early alpha version of <a href="http://swiftlet.org">Swiftlet</a>, an Open Source, light-weight PHP framework released under the <a href="http://www.gnu.org/licenses/gpl.html">GPL&nbsp;license</a>.</p>
<p>It&#8217;s targeted at developers who want to built simple websites that don&#8217;t require large and complex frameworks, but do want a solid base to work from. Swiftlet provides basic security features such as user input sanitizing,  is highly extensible thanks to the deeply integrated hook system, completely Object Oriented and separates logic from design&nbsp;(MVC).</p>
<p>Even the most basic features such as connecting to a database and output buffering are implemented as plug-ins. This means they can be modified, extended and removed without hacking into the core&nbsp;code.</p>
<p>Website:&nbsp;<a href="http://swiftlet.org/">http://swiftlet.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/04/swiftlet-%e2%80%94-light-weight-php-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: How not to pollute the global scope</title>
		<link>http://elbertf.com/2009/03/php-how-not-to-pollute-the-global-scope/</link>
		<comments>http://elbertf.com/2009/03/php-how-not-to-pollute-the-global-scope/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 04:04:27 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=62</guid>
		<description><![CDATA[An emerging trend in JavaScript is to wrap entire programs in a single object to prevent conflicts with other scripts. The same can be done with PHP, something I haven&#8217;t seen done very&#160;often. If you ever want to combine frameworks you may discover that they use similar names for some variables and functions. This can [...]]]></description>
			<content:encoded><![CDATA[<p>An emerging trend in JavaScript is to wrap entire programs in a single object to prevent conflicts with other scripts. The same can be done with PHP, something I haven&#8217;t seen done very&nbsp;often.</p>
<p><span id="more-62"></span></p>
<p>If you ever want to combine frameworks you may discover that they use similar names for some variables and functions. This can lead to unexpected output and security holes. If you&#8217;re planning on writing a framework of your own, consider wrapping it in an&nbsp;object.</p>
<p>In PHP5 it&#8217;s possible to pass objects by reference which means you can send an instance of a class to another class. This way you can nest classes and access the&nbsp;parent.</p>
<p><strong>core.php:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> core
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span>
        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a global variable.'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">const</span>
        CONST_VAR <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a constant.'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// This function is executed when core is initialized.</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;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'classes/foo.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">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: #000088;">$this</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;">// All the main functions go here.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>foo.php:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> foo
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$core</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$core</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Reference to the core object.</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">core</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$core</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> hello_world<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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">core</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">var</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this example &#8220;core&#8221; is the site&#8217;s main object containing global variables and functions. When we create an instance of class &#8220;foo&#8221;, we send a reference to the core object (&#8220;$this&#8221;) as a parameter. On a regular page the classes, variables and function can be accessed like&nbsp;this:</p>
<p><strong>page.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'classes/core.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$core</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> core<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: #000088;">$core</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">var</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// output: This is a global variable.</span>
<span style="color: #b1b100;">echo</span> core<span style="color: #339933;">::</span><span style="color: #004000;">CONST_VAR</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// output: This is a constant.</span>
&nbsp;
<span style="color: #000088;">$core</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hello_world</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;">// output: This is a global variable.</span></pre></div></div>

<p>This way there is only one variable in the global scope: $core. Another advantage is that all variables declared in core are global and accessible throughout the whole&nbsp;program.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/03/php-how-not-to-pollute-the-global-scope/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Tips for writing compact PHP code</title>
		<link>http://elbertf.com/2009/03/tips-for-writing-compact-php-code/</link>
		<comments>http://elbertf.com/2009/03/tips-for-writing-compact-php-code/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 08:34:16 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=30</guid>
		<description><![CDATA[Writing compact code can save you time. It&#8217;s not always recommended and often even strongly discouraged as it makes your code less readable, but for simple operations it can be more efficient. In this post I will give a few&#160;examples. 1. Drop&#160;braces Braces aren&#8217;t required in control structures with only one expression. Sometimes it makes [...]]]></description>
			<content:encoded><![CDATA[<p>Writing compact code can save you  time. It&#8217;s not always recommended and often even strongly discouraged as it makes your code less readable, but for simple operations it can be more efficient. In this post I will give a few&nbsp;examples.</p>
<p><span id="more-30"></span></p>
<h4>1. Drop&nbsp;braces</h4>
<p>Braces aren&#8217;t required in control structures with only one expression. Sometimes it makes sense to drop them. It&#8217;s <em>very easy to make mistakes</em> if you ever add code to the structure, I recommend only to do this when the expression is short and fits on a single&nbsp;line.</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$module</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>
    load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</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>
    error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No module'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$module</span> <span style="color: #009900;">&#41;</span> load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>           error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No module'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>2. Use ternary&nbsp;operators</h4>
<p>The above example can be made even more compact using <a href="http://en.wikipedia.org/wiki/%3F:">ternary&nbsp;operators</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$module</span> ? load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No module'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>3. Use &#8220;OR&#8221; instead of&nbsp;&#8220;IF&#8221;</h4>
<p>&#8220;OR&#8221; is the same as &#8220;or&#8221; and&nbsp;&#8221;||&#8221;.</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$foo</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$foo</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: #000088;">$bar</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$foo</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$bar</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>4. Don&#8217;t compare variables to&nbsp;booleans</h4>
<p>&#8220;if ( $foo == TRUE )&#8221; is the same as &#8220;if ( $foo )&#8221;. This shortcut can make your life as a programmer much&nbsp;easier:</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> has_value<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</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;">$var</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;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</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;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> has_value<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> bool <span style="color: #009900;">&#41;</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>5. Use default values for&nbsp;variables</h4>
<p>It&#8217;s usually a good idea to define important variables at the beginning of your script, instead of inside control structures (this could result in undefined variables later on). Another advantage is that you can often save an entire else-block as demonstrated in this&nbsp;example:</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$foo</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$foo is greater then 3.'</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;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$foo is lower than or equal to 3.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$foo is lower than or equal to 3.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$foo</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$foo is greater then 3.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>6. Assign variables inside&nbsp;conditions</h4>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$contents</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$contents</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><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: #b1b100;">echo</span> <span style="color: #000088;">$contents</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>7. Group variable&nbsp;declarations</h4>
<p>Instead of prefixing every single variable declaration in a class with &#8220;public&#8221;, &#8220;protected&#8221; or &#8220;private&#8221; keywords, group&nbsp;them:</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> db
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$tables</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> db
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span>
        <span style="color: #000088;">$query</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$result</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$tables</span>
        <span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Something similar can be done with regular variable declarations if they need to assign them the same&nbsp;value:</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span>    <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar</span>    <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$foobar</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span>    <span style="color: #339933;">=</span>
<span style="color: #000088;">$bar</span>    <span style="color: #339933;">=</span>
<span style="color: #000088;">$foobar</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>8. Merge arrays instead of assigning individual&nbsp;keys</h4>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item 1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'item 2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'item 3'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'item 4'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'item 5'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item 1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'item 2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'item 3'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'item 4'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'item 5'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Even&nbsp;shorter:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item 1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'item 2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$items</span> <span style="color: #339933;">+=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'item 3'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'item 4'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'item 5'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s it! Please share this post if you found it&nbsp;useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/03/tips-for-writing-compact-php-code/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

