<?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>Frank Verhoeven &#187; PHP</title>
	<atom:link href="http://www.frank-verhoeven.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.frank-verhoeven.com</link>
	<description>Webdevelopment Blog</description>
	<lastBuildDate>Wed, 21 Jul 2010 00:37:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Create an RSS Feed with WordPress</title>
		<link>http://www.frank-verhoeven.com/create-an-rss-feed-with-wordpress/</link>
		<comments>http://www.frank-verhoeven.com/create-an-rss-feed-with-wordpress/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 16:41:00 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.frank-verhoeven.com/?p=238</guid>
		<description><![CDATA[When you are busy creating a wonderful plugin for your WordPress blog, it might be possible that you need to create a RSS feed. For example, when creating a guestbook people could ask for a RSS feed to view the recent added posts and stay updated, or take a look at my own Community News [...]]]></description>
			<content:encoded><![CDATA[<p>When you are busy creating a wonderful plugin for your WordPress blog, it might be possible that you need to create a RSS feed. For example, when creating a guestbook people could ask for a RSS feed to view the recent added posts and stay updated, or take a look at my own <a href="http://www.frank-verhoeven.com/wordpress-plugin-fv-community-news/">Community News Plugin</a>, isn&#8217;t it great if it had a build-in RSS feed as well?</p>
<p>Well don&#8217;t worry I am busy to create that and it is almost finished. In the mean while, I wanted to share some stuff I learned while creating this RSS feed.</p>
<h3>The Beginning</h3>
<p>The first thing we need is a function that actually creates the RSS feed. This function doesn&#8217;t need any parameters and it also shouldn&#8217;t return anything.<br />
<span id="more-238"></span></p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpFunctionKeyword">function</span><span class="htmlText"> myGreatPluginRSSFeed</span><span class="phpOperator">(</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>
    <span class="phpComment">// Function content goes here.
</span><span class="phpOperator">}</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>This function must create the whole feed form the beginning to the end. So, you also have to include the xml/rss header. I ripped something from the default WordPress feed that should work for any blog.</p>
<pre class="php">
<span class="htmlOtherTag">&lt;rss version=<span class="htmlAttributeValue">&quot;2.0&quot;</span>
	xmlns:content=<span class="htmlAttributeValue">&quot;http://purl.org/rss/1.0/modules/content/&quot;</span>
	xmlns:dc=<span class="htmlAttributeValue">&quot;http://purl.org/dc/elements/1.1/&quot;</span>
	xmlns:atom=<span class="htmlAttributeValue">&quot;http://www.w3.org/2005/Atom&quot;</span>
	xmlns:sy=<span class="htmlAttributeValue">&quot;http://purl.org/rss/1.0/modules/syndication/&quot;</span>&gt;</span>
	<span class="htmlOtherTag">&lt;channel&gt;</span>
		<span class="htmlOtherTag">&lt;title&gt;</span><span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span><span class="htmlText"> bloginfo_rss</span><span class="phpOperator">(</span><span class="phpString">'name'</span><span class="phpOperator">)</span><span class="phpText">;</span><span class="htmlText"> wp_title_rss</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span> <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span><span class="htmlOtherTag">&lt;/title&gt;</span>
		<span class="htmlOtherTag">&lt;link&gt;</span><span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span><span class="htmlText"> bloginfo_rss</span><span class="phpOperator">(</span><span class="phpString">'url'</span><span class="phpOperator">)</span> <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span><span class="htmlOtherTag">&lt;/link&gt;</span>
		<span class="htmlOtherTag">&lt;atom:link href=<span class="htmlAttributeValue">&quot;<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span><span class="htmlText"> self_link</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span> <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>&quot;</span> rel=<span class="htmlAttributeValue">&quot;self&quot;</span> type=<span class="htmlAttributeValue">&quot;application/rss+xml&quot;</span> /&gt;</span>
		<span class="htmlOtherTag">&lt;description&gt;</span><span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span><span class="htmlText"> bloginfo_rss</span><span class="phpOperator">(</span><span class="phpString">"description"</span><span class="phpOperator">)</span> <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span><span class="htmlOtherTag">&lt;/description&gt;</span>
		<span class="htmlOtherTag">&lt;language&gt;</span><span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span> <span class="phpFunction">echo</span><span class="htmlText"> get_option</span><span class="phpOperator">(</span><span class="phpString">'rss_language'</span><span class="phpOperator">)</span><span class="phpText">;</span> <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span><span class="htmlOtherTag">&lt;/language&gt;</span>
		<span class="htmlOtherTag">&lt;sy:updatePeriod&gt;</span><span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span> <span class="phpFunction">echo</span><span class="htmlText"> apply_filters</span><span class="phpOperator">(</span> <span class="phpString">'rss_update_period'</span><span class="phpText">,</span> <span class="phpString">'hourly'</span> <span class="phpOperator">)</span><span class="phpText">;</span> <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span><span class="htmlOtherTag">&lt;/sy:updatePeriod&gt;</span>
		<span class="htmlOtherTag">&lt;sy:updateFrequency&gt;</span><span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span> <span class="phpFunction">echo</span><span class="htmlText"> apply_filters</span><span class="phpOperator">(</span> <span class="phpString">'rss_update_frequency'</span><span class="phpText">,</span> <span class="phpString">'<span class="phpNumber">1</span>'</span> <span class="phpOperator">)</span><span class="phpText">;</span> <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span><span class="htmlOtherTag">&lt;/sy:updateFrequency&gt;</span>
		<span class="htmlOtherTag">&lt;item&gt;</span>
		<span class="htmlOtherTag">&lt;/item&gt;</span>
	<span class="htmlOtherTag">&lt;/channel&gt;</span>
<span class="htmlOtherTag">&lt;/rss&gt;</span>
</pre>
<p>Now, you could see the &lt;item&gt; and &lt;/item&gt; tags, that is where we are going to add our feed items. From this moment I assume that all our RSS data is located in an array like this one.</p>
<pre class="php">
<span class="phpIdentifier">$rssItems</span> <span class="phpOperator">=</span> <span class="phpFunction">array</span><span class="phpOperator">(</span>
	<span class="phpString">'item_1'</span><span class="phpOperator"><span class="phpOperator">=</span><span class="phpOperator">&gt;</span></span><span class="phpFunction">array</span><span class="phpOperator">(</span>
		<span class="phpString">'Title'</span><span class="phpText">,</span>
		<span class="phpString">'Link'</span><span class="phpText">,</span>
		<span class="phpString">'Author'</span><span class="phpText">,</span>
		<span class="phpString">'Date'</span><span class="phpText">,</span>
		<span class="phpString">'Description'</span><span class="phpText">,</span>
		<span class="phpString">'Content'</span>
		<span class="phpOperator">)</span><span class="phpText">,</span>
	<span class="phpString">'item_2'</span><span class="phpOperator"><span class="phpOperator">=</span><span class="phpOperator">&gt;</span></span><span class="phpFunction">array</span><span class="phpOperator">(</span>
		<span class="phpString">'Title'</span><span class="phpText">,</span>
		<span class="phpString">'Link'</span><span class="phpText">,</span>
		<span class="phpString">'Author'</span><span class="phpText">,</span>
		<span class="phpString">'Date'</span><span class="phpText">,</span>
		<span class="phpString">'Description'</span><span class="phpText">,</span>
		<span class="phpString">'Content'</span>
		<span class="phpOperator">)</span>
	<span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p>And so on. For adding the items to our feed, we simply use a foreach loop.</p>
<pre class="php">
<span class="phpKeyword">
foreach </span><span class="phpOperator">(</span><span class="phpIdentifier">$rssItems</span><span class="phpKeyword"> as </span><span class="phpIdentifier">$item</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>
	<span class="phpFunction">echo</span> <span class="phpString">'<span class="phpOperator">&lt;</span>item<span class="phpOperator">&gt;</span>'</span><span class="phpText">;</span>
	<span class="phpFunction">echo</span> <span class="phpString">'<span class="phpOperator">&lt;</span>title<span class="phpOperator">&gt;</span>'</span> <span class="phpOperator">.</span> <span class="phpIdentifier">$item</span><span class="phpOperator">[</span><span class="phpString">'Title'</span><span class="phpOperator">]</span> <span class="phpOperator">.</span> <span class="phpString">'<span class="phpOperator">&lt;</span>/title<span class="phpOperator">&gt;</span>'</span><span class="phpText">;</span>
	<span class="phpFunction">echo</span> <span class="phpString">'<span class="phpOperator">&lt;</span>link<span class="phpOperator">&gt;</span>'</span> <span class="phpOperator">.</span> <span class="phpIdentifier">$item</span><span class="phpOperator">[</span><span class="phpString">'Link'</span><span class="phpOperator">]</span> <span class="phpOperator">.</span> <span class="phpString">'<span class="phpOperator">&lt;</span>/link<span class="phpOperator">&gt;</span>'</span><span class="phpText">;</span>
	<span class="phpFunction">echo</span> <span class="phpString">'<span class="phpOperator">&lt;</span>dc<span class="phpOperator">:</span>creator<span class="phpOperator">&gt;</span>'</span> <span class="phpOperator">.</span> <span class="phpIdentifier">$item</span><span class="phpOperator">[</span><span class="phpString">'Author'</span><span class="phpOperator">]</span> <span class="phpOperator">.</span> <span class="phpString">'<span class="phpOperator">&lt;</span>/dc<span class="phpOperator">:</span>creator<span class="phpOperator">&gt;</span>'</span><span class="phpText">;</span>
	<span class="phpFunction">echo</span> <span class="phpString">'<span class="phpOperator">&lt;</span>pubDate<span class="phpOperator">&gt;</span>'</span> <span class="phpOperator">.</span> mysql2date<span class="phpOperator">(</span><span class="phpString">'D<span class="phpText">,</span> d M Y H<span class="phpOperator">:</span>i<span class="phpOperator">:</span>s <span class="phpOperator">+</span>0000'</span><span class="phpText">,</span> <span class="phpIdentifier">$item</span><span class="phpOperator">[</span><span class="phpString">'Date'</span><span class="phpOperator">]</span><span class="phpOperator">)</span> <span class="phpOperator">.</span> <span class="phpString">'<span class="phpOperator">&lt;</span>/pubDate<span class="phpOperator">&gt;</span>'</span><span class="phpText">;</span>
	<span class="phpFunction">echo</span> <span class="phpString">'<span class="phpOperator">&lt;</span>description<span class="phpOperator">&gt;</span>'</span> <span class="phpOperator">.</span> <span class="phpIdentifier">$item</span><span class="phpOperator">[</span><span class="phpString">'Description'</span><span class="phpOperator">]</span> <span class="phpOperator">.</span> <span class="phpString">'<span class="phpOperator">&lt;</span>/description<span class="phpOperator">&gt;</span>'</span><span class="phpText">;</span>
	<span class="phpFunction">echo</span> <span class="phpString">'<span class="phpOperator">&lt;</span>content<span class="phpOperator">:</span>encoded<span class="phpOperator">&gt;</span><span class="phpOperator">&lt;</span><span class="phpOperator">!</span><span class="phpOperator">[</span>CDATA<span class="phpOperator">[</span>'</span> <span class="phpOperator">.</span> <span class="phpIdentifier">$item</span><span class="phpOperator">[</span><span class="phpString">'Content'</span><span class="phpOperator">]</span> <span class="phpOperator">.</span> <span class="phpString">'<span class="phpOperator">]</span><span class="phpOperator">]</span><span class="phpOperator">&gt;</span><span class="phpOperator">&lt;</span>/content<span class="phpOperator">:</span>encoded<span class="phpOperator">&gt;</span>'</span><span class="phpText">;</span>
	<span class="phpFunction">echo</span> <span class="phpString">'<span class="phpOperator">&lt;</span>/item<span class="phpOperator">&gt;</span>'</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
<p>The mysql2date function is a WordPress function that converts our date to the format we want for our feed. The input should be the date in the following format: <code>d-m-Y H:i:s</code>. See <a href="http://php.net/date">php.net</a> for more information about the date formatting.</p>
<p>Now we have finished our function, we have only one thing to do.</p>
<h3>Adding the RSS Feed to WordPress</h3>
<p>To add the feed, we could use one simple function, build-in in WordPress since version <code>2.1.0</code>. This is the <code>add_feed</code> function. This function has two parameters.</p>
<ul>
<li>$feedname; The name of your feed.</li>
<li>$function; The function that creates the feed. (The one we created above)</li>
</ul>
<p>So lets say we name our feed: &#8216;guestbook&#8217;, then we should call the <code>add_feed</code> function this way:</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpComment">// Add our<span class="phpKeyword"> new </span><span class="htmlText">RSS feed</span><span class="phpOperator">.</span>
</span><span class="htmlText">add_feed</span><span class="phpOperator">(</span><span class="phpString">'guestbook'</span><span class="phpText">,</span> <span class="phpString">'myGreatPluginRSSFeed'</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>Now our feed is added and working! One thing you might be asking yourself is: Where could I find my feed? Well, that&#8217;s simple.<br />
If you are using permalinks on your blog (could be checked this way: <code>$wp_rewrite->using_permalinks()</code>), then your feed is located at: http://your.website.com/[feedname].<br />
If you don&#8217;t use permalinks, your feed is located at: http://your.website.com/?feed=[feedname].</p>
<h3>Done!</h3>
<p>After all our hard work, we have created one beautiful RSS 2.0 feed, working with WordPress. Any questions? Please <a href="http://www.frank-verhoeven.com/create-an-rss-feed-with-wordpress/#respond">leave a comment</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.frank-verhoeven.com/create-an-rss-feed-with-wordpress/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Using Sessions in WordPress</title>
		<link>http://www.frank-verhoeven.com/using-session-in-wordpress/</link>
		<comments>http://www.frank-verhoeven.com/using-session-in-wordpress/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 13:03:54 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.frank-verhoeven.com/?p=158</guid>
		<description><![CDATA[Yesterday, when I was creating a new plugin for my friend Stefan Vervoort, I needed sessions to work. Unfortunately, WordPress doesn&#8217;t support them!?? I searched the whole source code of this great piece of blogging software, and not on one single line I found even one session. Also the session_start() function is not called, but [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, when I was creating a new plugin for my friend <a href="http://www.divitodesign.com/">Stefan Vervoort</a>, I needed sessions to work. Unfortunately, WordPress doesn&#8217;t support them!??</p>
<p>I searched the whole source code of this great piece of blogging software, and not on one single line I found even one session. Also the <a href="http://www.php.net/session_start">session_start()</a> function is not called, but I still needed my sessions to work <img src='http://www.frank-verhoeven.com/wp-includes/images/smilies/icon_confused.gif' alt=':-?' class='wp-smiley' />  .</p>
<p>So I started searching Google for a fix of my problem. I found a lot of people asking the same question: &#8220;Why don&#8217;t sessions work in WordPress?&#8221; Finally I found a solution to fix this little issue and guess what, it is a simple one.<br />
<span id="more-158"></span></p>
<h3>The Solution</h3>
<p>The only thing you have to do is call session_start(); before any output is send to the client. Now you might think: Nice, but what happens when I upgrade my WordPress installation to the latest version? Well, yes, your changes will be lost. That is the reason why we first should think of where to add these changes..</p>
<p>Normally upgrading your installation will replace all files, except one. Yes, it is the wp-config.php file. And even better, there isn&#8217;t send any output to a client yet, when this file is loaded.</p>
<p>So, we add the next lines of code to our wp-config.php file:</p>
<pre class="php">
<span class="phpComment">// Make the<span class="phpKeyword"> use </span>of sessions possible.
</span>>if <span class="phpOperator">(</span><span class="phpOperator">!</span><span class="phpFunction">session_id</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpOperator">)</span>
<span class="phpFunction">session_start</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p>And sessions are enabled on your blog!<br />
I think the best place to add these lines is at the top of the config file, so immediately after the php starting tag (&lt;?php).</p>
<p>I hope this will help you out when facing the same problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.frank-verhoeven.com/using-session-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>52</slash:comments>
		</item>
		<item>
		<title>New PHP Highlighter In Town</title>
		<link>http://www.frank-verhoeven.com/new-php-highlighter-in-town/</link>
		<comments>http://www.frank-verhoeven.com/new-php-highlighter-in-town/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 09:31:57 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress Plugins]]></category>

		<guid isPermaLink="false">http://www.frank-verhoeven.com/?p=138</guid>
		<description><![CDATA[Hey everyone, As you could have read here, the current release of my code highlighter has some trouble with highlighting PHP on servers running PHP version 4. I told you I should try to fix the problem and include a better PHP highlighter in the next release. I worked very hard this morning to get [...]]]></description>
			<content:encoded><![CDATA[<p>Hey everyone,</p>
<p>As you could have read <a href="http://www.frank-verhoeven.com/wordpress-plugin-fv-code-highlighter/#comment-11">here</a>, the current release of my code highlighter has some trouble with highlighting PHP on servers running PHP version 4. I told you I should try to fix the problem and include a better PHP highlighter in the next release.</p>
<p>I worked very hard this morning to get PHP fixed, and I&#8217;m proud to announce that PHP is almost working the way it should. That means: including servers running PHP4.</p>
<p>You want an example? Sure, here it is:</p>
<h3>PHP Code Like It Should</h3>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpFunction">mysql_connect</span> <span class="phpOperator">(</span><span class="phpIdentifier">$host</span><span class="phpText">,</span> <span class="phpIdentifier">$user</span><span class="phpText">,</span> <span class="phpIdentifier">$password</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpIdentifier">$result</span> <span class="phpOperator">=</span> <span class="phpFunction">mysql_db_query</span> <span class="phpOperator">(</span><span class="phpString">'Database'</span><span class="phpText">,</span> <span class="phpString">'select * from table'</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
while </span><span class="phpOperator">(</span><span class="phpIdentifier">$row</span> <span class="phpOperator">=</span> <span class="phpFunction">mysql_fetch_assoc</span> <span class="phpOperator">(</span><span class="phpIdentifier">$result</span><span class="phpOperator">)</span><span class="phpOperator">)</span>
<span class="phpOperator">{</span>
	<span class="phpFunction">echo</span> <span class="phpIdentifier">$row</span><span class="phpOperator">[</span><span class="phpString">'user_id'</span><span class="phpOperator">]</span><span class="phpText">;</span>
	<span class="phpFunction">echo</span> <span class="phpIdentifier">$row</span><span class="phpOperator">[</span><span class="phpString">'fullname'</span><span class="phpOperator">]</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
<span class="phpFunction">mysql_free_result</span> <span class="phpOperator">(</span><span class="phpIdentifier">$result</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<h3>So, What&#8217;s Next?</h3>
<p>After this highlighter is finished, I create one for JavaScript and properly one for Java. As soon as they all work the way it should, I will release version 1.1.</p>
<p>See you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.frank-verhoeven.com/new-php-highlighter-in-town/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Short If-Else Structure</title>
		<link>http://www.frank-verhoeven.com/short-if-else-structure/</link>
		<comments>http://www.frank-verhoeven.com/short-if-else-structure/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 17:33:54 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.frank-verhoeven.com/?p=121</guid>
		<description><![CDATA[In addition to the enlarged if &#8211; else statement that you will use in most cases, there is also a short structure for an if &#8211; else statement. This format uses the so-called &#8220;ternary operator &#8216;. The syntax of this shorthand structure is as follows: $var = [condition] ? [true] : [false]; Condition = The [...]]]></description>
			<content:encoded><![CDATA[<p>In addition to the enlarged if &#8211; else statement that you will use in most cases, there is also a short structure for an if &#8211; else statement. This format uses the so-called &#8220;ternary operator &#8216;. The syntax of this shorthand structure is as follows:</p>
<pre class="php">
<span class="phpIdentifier">$var</span> <span class="phpOperator">=</span> <span class="phpOperator">[</span>condition<span class="phpOperator">]</span> <span class="phpOperator">?</span> <span class="phpKeyword"><span class="phpOperator">[</span>true<span class="phpOperator">]</span></span> <span class="phpOperator">:</span> <span class="phpKeyword"><span class="phpOperator">[</span>false<span class="phpOperator">]</span></span><span class="phpText">;</span>
</pre>
<ol>
<li>Condition = The condition which must be met.</li>
<li>True = Executed if the condition is met.</li>
<li>False = Executed if the condition failes.</li>
</ol>
<p><span id="more-121"></span><br />
Thus, the above statement means the same as this enlarged structure:</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpKeyword">
if </span><span class="phpOperator">(</span><span class="htmlText">condition</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>
    <span class="phpIdentifier">$var</span> <span class="phpOperator">=</span> <span class="phpKeyword"><span class="phpOperator">[</span><span class="htmlText">true</span><span class="phpOperator">]</span></span><span class="phpText">;</span>
<span class="phpOperator">}</span><span class="phpKeyword"> else </span><span class="phpOperator">{</span>
    <span class="phpIdentifier">$var</span> <span class="phpOperator">=</span> <span class="phpKeyword"><span class="phpOperator">[</span><span class="htmlText">false</span><span class="phpOperator">]</span></span><span class="phpText">;</span>
<span class="phpOperator">}</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<h3>Examples</h3>
<p>Let&#8217;s take a look at some example.</p>
<p>Filled in a name?</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpIdentifier">$name</span> <span class="phpOperator">=</span> <span class="phpFunction">isset</span><span class="phpOperator">(</span><span class="phpScriptVar"><span class="phpIdentifier">$_POST</span></span><span class="phpOperator">[</span><span class="phpString">'name'</span><span class="phpOperator">]</span><span class="phpOperator">)</span><span class="phpOperator">?</span><span class="phpScriptVar"><span class="phpIdentifier">$_POST</span></span><span class="phpOperator">[</span><span class="phpString">'name'</span><span class="phpOperator">]</span><span class="phpOperator">:</span><span class="phpString">'Unknown'</span><span class="phpText">;</span>
<span class="phpComment">// If <span class="phpScriptVar"><span class="phpIdentifier">$_POST</span></span><span class="phpOperator">[</span><span class="phpString">'name'</span><span class="phpOperator">]</span><span class="htmlText"> exists</span><span class="phpText">,</span><span class="htmlText"> we</span><span class="phpKeyword"> use </span><span class="htmlText">that</span><span class="phpKeyword"> for </span><span class="htmlText">the name</span><span class="phpText">,</span><span class="phpKeyword"> else </span><span class="htmlText">we</span><span class="phpKeyword"> use </span><span class="htmlText">Unknown</span><span class="phpOperator">.</span>
</span><span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>We can also use it to easily echo stuff:</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpIdentifier">$fruit</span> <span class="phpOperator">=</span> <span class="phpString">'apple'</span><span class="phpText">;</span>
<span class="phpFunction">echo</span> <span class="phpOperator">(</span><span class="phpString">'pear'</span> <span class="phpOperator"><span class="phpOperator">=</span>=</span> <span class="phpIdentifier">$fruit</span><span class="phpOperator">)</span><span class="phpOperator">?</span><span class="phpString">'pear'</span><span class="phpOperator">:</span><span class="phpString">'apple'</span><span class="phpText">;</span>
<span class="phpComment">// Will <span class="phpFunction">echo</span> apple
</span><span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>It is even possible to use php-functions inside it:</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpIdentifier">$input</span> <span class="phpOperator">=</span> <span class="phpString">'Just a string to be hashed'</span><span class="phpText">;</span>
<span class="phpIdentifier">$hashMethod</span> <span class="phpOperator">=</span> <span class="phpString">'<span class="phpFunction">sha1</span>'</span><span class="phpText">;</span>
<span class="phpIdentifier">$hash</span> <span class="phpOperator">=</span> <span class="phpOperator">(</span><span class="phpString">'<span class="phpFunction">sha1</span>'</span> <span class="phpOperator"><span class="phpOperator">=</span>=</span> <span class="phpIdentifier">$hashMethod</span><span class="phpOperator">)</span><span class="phpOperator">?</span><span class="phpFunction">sha1</span><span class="phpOperator">(</span><span class="phpIdentifier">$input</span><span class="phpOperator">)</span><span class="phpOperator">:</span><span class="phpFunction">md5</span><span class="phpOperator">(</span><span class="phpIdentifier">$input</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpComment">// <span class="phpIdentifier">$hash</span><span class="htmlText"> will contain an </span><span class="phpFunction">sha1</span><span class="htmlText"> hash of </span><span class="phpIdentifier">$input</span>
</span><span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<h3>Conclusion</h3>
<p>The short if-else structure is a good way to keep your code organized. However, it is not possible to use such a thing as: &#8216;elseif&#8217;.<br />
I use it all the time to assign values to variables, with just one rule of code, I&#8217;m sure my vars have a proper value. Especially when using forms, this is a very useful function of PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.frank-verhoeven.com/short-if-else-structure/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
