<?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; tips</title>
	<atom:link href="http://elbertf.com/tag/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://elbertf.com</link>
	<description>Insights and Updates from a Tech Geek</description>
	<lastBuildDate>Sun, 15 Apr 2012 21:46:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Safer web forms with security tokens</title>
		<link>http://elbertf.com/2009/11/safer-web-forms-with-security-tokens/</link>
		<comments>http://elbertf.com/2009/11/safer-web-forms-with-security-tokens/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 08:24:31 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=367</guid>
		<description><![CDATA[A common issue with many web applications is their vulnerability for Cross-Site Request Forgery, or XSRF. It allows a hacker to send a malicious request to a website with an other user&#8217;s privileges. Here&#8217;s how it&#160;works: A hacker creates a page with a form that submits data to&#160;example.com. An administrator from example.com is tricked into [...]]]></description>
			<content:encoded><![CDATA[<p>
A common issue with many web applications is their vulnerability for <a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">Cross-Site Request Forgery</a>, or XSRF. It allows a hacker to send a malicious request to a website with an other user&#8217;s privileges. Here&#8217;s how it&nbsp;works:
</p>
<ul>
<li>
A hacker creates a page with a form that submits data to&nbsp;<em>example.com</em>.
</li>
<li>
An administrator from <em>example.com</em> is tricked into visiting the page, the form is submitted using&nbsp;JavaScript.
</li>
<li>
The data is handled by <em>example.com</em> as if it came from the administrator (because it&nbsp;did).
</li>
</ul>
<p>
This allows a hacker to perform administrative tasks on <em>example.com</em> like editing pages or deleting&nbsp;users.
</p>
<p><span id="more-367"></span></p>
<h4>Solution</h4>
<p>
Probably the best solution to this problem is using a <a href="http://en.wikipedia.org/wiki/Security_token">security token</a>. This is a code (usually a <a href="http://en.wikipedia.org/wiki/Hash_function">hash</a>) that is send with the form in a hidden field and is only valid for a specific user and a certain period of&nbsp;time.
</p>
<p>
I recommend using a <a href="http://en.wikipedia.org/wiki/SHA_hash_functions">SHA1</a> hash created from these&nbsp;components:
</p>
<ul>
<li>
Information about the user (IP address, user agent,&nbsp;username).
</li>
<li>
Information about the server (hostname, software&nbsp;version).
</li>
<li>
Information about the website (database name, table&nbsp;prefix).
</li>
<li>
Information that expires (user&#8217;s session&nbsp;id).
</li>
</ul>
<p>
This will result in an unpredictable and seemingly random hash that is still verifiable by the server and difficult to&nbsp;fake.
</p>
<p>
After the form is submitted the hash is re-created and compared to the token that was send with the form. Only if the hashes match the form is processed, otherwise an error message is displayed. This way even if a hacker manages to find the user&#8217;s token the request will&nbsp;fail.
</p>
<p><strong>Example&nbsp;(PHP/HTML):</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$authToken</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sha1</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span>
  <span style="color: #990000;">phpversion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span>
  <span style="color: #000088;">$dbName</span> <span style="color: #339933;">.</span>
  <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span>
  <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_USER_AGENT'</span><span style="color: #009900;">&#93;</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'auth_token'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'auth_token'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$authToken</span> <span style="color: #009900;">&#41;</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: #b1b100;">else</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Display error message</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;form id=&quot;form&quot; method=&quot;post&quot; action=&quot;./&quot;&gt;
  &lt;fieldset&gt;
    &lt;label for=&quot;name&quot;&gt;Name:&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; value=&quot;&quot;/&gt;
  &lt;/fieldset&gt;
  &lt;fieldset&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;auth_token&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$authToken</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;/&gt;
&nbsp;
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;/&gt;
  &lt;/fieldset&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>Note that this only really works for POST requests. For this reason GET (regular links) should never be used to pass information that is used for administrative tasks. AJAX requests should also <em>always</em> use&nbsp;POST.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/11/safer-web-forms-with-security-tokens/feed/</wfw:commentRss>
		<slash:comments>5</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>7</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>5</slash:comments>
		</item>
	</channel>
</rss>

