<?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>Not A n00b &#187; if</title>
	<atom:link href="http://notan00b.com/tag/if/feed/" rel="self" type="application/rss+xml" />
	<link>http://notan00b.com</link>
	<description>Tutorials, Scripts, and Rants</description>
	<lastBuildDate>Wed, 12 May 2010 08:08:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP/Perl If trick using  ? and :</title>
		<link>http://notan00b.com/2009/07/php-if-trick-using-and/</link>
		<comments>http://notan00b.com/2009/07/php-if-trick-using-and/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 20:14:38 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[:]]></category>
		<category><![CDATA[?]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[colon]]></category>
		<category><![CDATA[else]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[inline]]></category>
		<category><![CDATA[one line]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[question mark]]></category>
		<category><![CDATA[statement]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=107</guid>
		<description><![CDATA[Most experienced programmers already know this, but this is a quick trick that will save you a few lines of code if you have a really simple if-statement to display one thing or another.  This works with both PHP and Perl and will help you cut down some coding time.  For this post [...]]]></description>
			<content:encoded><![CDATA[<p>Most experienced programmers already know this, but this is a quick trick that will save you a few lines of code if you have a really simple if-statement to display one thing or another.  This works with both PHP and Perl and will help you cut down some coding time.  For this post we will show PHP.  Lets begin:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$id</span><span style="color: #339933;">=</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: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Welcome Guest, please register to use this page.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Welcome back!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This is fairly simple to understand, and most people have statements like this through out any webpage.  There is a trick that makes the coding much shorter.  I have no idea what it is called, so we will call it an inline-if-statement.  It utilizes the question mark (?) and colon (:) to execute simple if statements.  Lets rewrite the above code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$id</span><span style="color: #339933;">=</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: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Welcome &quot;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span>?<span style="color: #0000ff;">&quot;Guest, please register to use this page.&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;back!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>We summed up those 4 lines into one line of code.  I typically encase the statement in parenthesis, but you don&#8217;t need to.  The first part is the if-statement.  <code>If($id==1)</code> becomes <code>$id==1?</code>. Then if $id was equal to 1, it would append <code>"Guest, please register to use this page."</code> to <code>"Welcome "</code>.  <code>else</code> is now just a colon <code>:</code> and separates the two values.</p>
<p>Why is this useful?  Well its quicker code.  4 lines of code is now compressed to a single line.</p>
<p>Why would you not use it?  Well if you need to do other things within the if-statement.  This is strictly for displaying data.  If you need to change variable values, or call a function, then you would definitely not use this.  You would also not use this if you have &#8220;else if&#8221; statements.  This is strictly, if-else.</p>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/07/php-if-trick-using-and/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
