<?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>PHP Software Developing &#187; ternary operators</title>
	<atom:link href="http://www.phpdeveloping.co.za/tag/ternary-operators/feed" rel="self" type="application/rss+xml" />
	<link>http://www.phpdeveloping.co.za</link>
	<description>for the love of PHP Development</description>
	<lastBuildDate>Tue, 29 Sep 2009 15:38:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ternary Operators</title>
		<link>http://www.phpdeveloping.co.za/performance/ternary-operators.html</link>
		<comments>http://www.phpdeveloping.co.za/performance/ternary-operators.html#comments</comments>
		<pubDate>Wed, 22 Jul 2009 15:36:49 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Performance]]></category>
		<category><![CDATA[ternary operators]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=3</guid>
		<description><![CDATA[Instead of the normal if-else statements that most of us are use to, why not start using ternary operators.
Normal if-else statement:

if &#40;empty&#40;$v&#41;&#41; &#123;
$action = 'default';
&#125; else &#123;
$action = $v;
&#125;

Replacing the above 5 lines of code with just one line of code:

$action = &#40;empty&#40;$v&#41;&#41; ? 'default' : $v;

This might not increase the actual performance of the [...]]]></description>
			<content:encoded><![CDATA[<p>Instead of the normal if-else statements that most of us are use to, why not start using ternary operators.</p>
<p>Normal if-else statement:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'default'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Replacing the above 5 lines of code with just one line of code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span></pre></div></div>

<p>This might not increase the actual performance of the execution, but it does use fewer lines to code the same thing as well as making it easier to read.  Make sure not to use more than one ternary operator in a single statement, as PHP doesn’t always know what to do in those situations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/performance/ternary-operators.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
