<?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>mouse pi-lot &#187; php</title>
	<atom:link href="http://mousepilot.co.uk/topics/code/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://mousepilot.co.uk</link>
	<description>–noun: A computer user lacking in any recognisable skills&#34;</description>
	<lastBuildDate>Thu, 03 Feb 2011 09:48:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Handy code: ifelse()</title>
		<link>http://mousepilot.co.uk/2010/09/handy-code-ifelse/</link>
		<comments>http://mousepilot.co.uk/2010/09/handy-code-ifelse/#comments</comments>
		<pubDate>Sat, 25 Sep 2010 13:07:29 +0000</pubDate>
		<dc:creator>Simon Stevens</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://mousepilot.co.uk/?p=29</guid>
		<description><![CDATA[As with most web developers about half of my code is collecting information via forms (the other half is presenting that information). When a form is submitted there are a variety of variables that I might want to display back &#8230; <a href="http://mousepilot.co.uk/2010/09/handy-code-ifelse/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As with most web developers about half of my code is collecting information via forms (the other half is presenting that information).  When a form is submitted there are a variety of variables that I might want to display back to the user in the form elements.  The code snippet below simplifies how I handle which variable to display.</p>
<p>Previously my code for an input field would have used a lot of nested inline if statements to determine which variable to fill the field with.  It might have looked something like this:</p>
<p><code>echo '&lt;input type="text" name="email"  value="' . (($_POST['email']) ? $_POST['email'] : (($user['email']) ? $user['email'] : 'Email') . '" /&gt;';</code></p>
<p>The code above would show the posted email address first, and if that&#8217;s not present defer to one from the database.  If neither of those are present then it would display a label, &#8220;Email&#8221; in this case.</p>
<p>That&#8217;s a nightmare to manage, and it looks nasty.  So I wrote a function called postOrNot($post, $other) which simplified the inline if statement a touch.  If the first variable was present, it was displayed, if not then it would display the 2nd.</p>
<p>With the introduction of &#8220;func_get_args()&#8221; to the function it can handle any number of arguments, falling over to each one in turn, and finally returning false if none are present.</p>
<p><code>function ifelse(){<br />
    if(func_num_args() < 2) trigger_error("ifelse() requires 2 or more arguements", E_USER_ERROR);<br />
    else {<br />
        $args = func_get_args();<br />
        foreach($args as $arg){<br />
            if($arg) return $arg;<br />
        }<br />
        return false;<br />
    }<br />
}</code></p>
<p>It's a function that I use pretty much every day and saves me lines and lines of code.</p>
]]></content:encoded>
			<wfw:commentRss>http://mousepilot.co.uk/2010/09/handy-code-ifelse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

