<?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>Ezzatron &#187; wish list</title>
	<atom:link href="http://ezzatron.com/tag/wish-list/feed/" rel="self" type="application/rss+xml" />
	<link>http://ezzatron.com</link>
	<description>Bringing together the fine arts of programming and death metal.</description>
	<lastBuildDate>Sat, 07 Aug 2010 04:41:31 +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>Revisited: foreachif()</title>
		<link>http://ezzatron.com/2009/07/22/foreachif-revisited/</link>
		<comments>http://ezzatron.com/2009/07/22/foreachif-revisited/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 07:39:28 +0000</pubDate>
		<dc:creator>Erin</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[wish list]]></category>

		<guid isPermaLink="false">http://ezzatron.com/?p=107</guid>
		<description><![CDATA[A while ago I blogged about my desire for a foreachif() construct in PHP. A recent comment from a reader made me re-think the concept and consider ways in which it could be implemented elegantly, without the need for changes to PHP&#8216;s syntax. Why implement it in PHP code rather than make a change to [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I blogged about <a title="PHP wish list - part 1: foreachif()" href="http://ezzatron.com/2008/12/04/php-wishlist-part-1-foreachif/">my desire for a foreachif() construct in PHP</a>. A <a href="http://ezzatron.com/2008/12/04/php-wishlist-part-1-foreachif/comment-page-1/#comment-94">recent comment</a> from a reader made me re-think the concept and consider ways in which it could be implemented elegantly, without the need for changes to <a title="PHP: Hypertext Preprocessor" href="http://php.net/">PHP</a>&#8216;s syntax.</p>
<p><span id="more-107"></span></p>
<p>Why implement it in PHP code rather than make a change to the syntax itself? Well, the reason is fairly simple; having too many syntaxes for the same thing can make code very difficult to read and understand unless the reader is a guru in that particular language. Anyone who has been forced to use <a title="Perl" href="http://www.perl.org/">Perl</a> will know what I mean.</p>
<p>The above comment suggested the use of the <strong>eval()</strong> function. Whilst this would work, I don&#8217;t particularly like the idea for a number of reasons:</p>
<ul>
<li>If there is an error in the eval&#8217;d code, this will show up in a back-trace as occurring in eval&#8217;d code rather than pointing to the actual problem itself &#8211; messy.</li>
<li>If like me, you are used to a nice, cushy IDE, you&#8217;ll know that almost all of your code completion / highlighting immediately goes out the window inside of a string.</li>
<li>I try to avoid <strong>eval()</strong> as a rule of thumb. It just makes me feel <em>dirty</em>.</li>
</ul>
<p>I thought I might try a similar solution however, using PHP 5.3&#8242;s new <a title="PHP: Anonymous functions - Manual" href="http://php.net/closures">closures</a> feature. Closures (A.K.A. <em>anonymous functions</em>) will most likely be familiar to anyone who has used JavaScript in any serious fashion, so I will not go into them here. If you are unfamiliar with the concept, please <a title="PHP: Anonymous functions - Manual" href="http://php.net/closures">RTFM</a>.</p>
<p>In order to make this work there are a few things we need:</p>
<ul>
<li>An array to iterate over</li>
<li>A condition of some sort to evaluate on each loop</li>
<li>A callback to execute if the condition evaluates to true</li>
<li>Some measure of control over variable names passed to the callback would also be nice</li>
</ul>
<p>The bottom line is that it is possible, but it&#8217;s nowhere near as nice, or elegant as I had hoped.</p>
<h2>Method 1: using closures</h2>
<p>Using nothing but the new closures syntax I was able to come up with the following reasonable-looking foreachif() function:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> foreachif<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$condition</span><span style="color: #339933;">,</span> <span style="color: #000088;">$callback</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/is_callable"><span style="color: #990000;">is_callable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$condition</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'condition must be a valid callback'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/is_callable"><span style="color: #990000;">is_callable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$callback</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'callback must be a valid callback'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$condition</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$callback</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This seemed fine until the time came to actually <em>use</em> the thing. Reproducing one of the examples from the aforementioned article resulted in this mess:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
foreachif <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Not particularly readable huh? Downright horrible is more like it.</p>
<h2>Method 2: mixed closures and eval&#8217;d strings</h2>
<p>The above mess made me think perhaps I could just use eval() for the condition &#8211; perhaps this would help remove some clutter from the statement? Well, yes it does, but it also presents its own problems. Here is the new function:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> foreachif<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$condition</span><span style="color: #339933;">,</span> <span style="color: #000088;">$callback</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/is_callable"><span style="color: #990000;">is_callable</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$callback</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'callback must be a valid callback'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$$value_name</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/eval"><span style="color: #990000;">eval</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\$</span>run = <span style="color: #006699; font-weight: bold;">$condition</span>;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$run</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$callback</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$$value_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>In order to retain flexibility in naming the variable used to store the current iteration value, I was forced to pass the variable name as a string. I didn&#8217;t like this at all, and the situation would only get worse if you wanted to be able to choose the name of the array <em>key </em>inside the condition also.</p>
<p>For the sake of completeness, here is an example the utilises the above function:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
foreachif <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'this_value'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'$this_value % 2 == 0'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this_value</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this_value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Then suddenly I realised I was forgetting a very, very important point; <em>variable scope</em>.</p>
<h2>The big problem</h2>
<p>What neither of the above implementations give you is access to any variables defined outside of the closure / eval&#8217;d code itself. Granted this could be worked around by grabbing <em>every</em> defined variable and re-defining them within the scope of the loop content, but this would almost certainly have major performance issues when used in any real-world situation.</p>
<p>At this point I basically abandoned the search for a function-based implementation and started considering PHP syntax alternatives, which turned out to be a much more elegant solution in the end&#8230;</p>
<h2>Method 3: using existing PHP syntax in alternative ways</h2>
<p>Starting with the ultra-obvious solution gives us the following:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</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;">$value</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span></div></div>
<p>There is also PHP&#8217;s alternate syntax for these structures to consider, and I think the following actually comes out looking quite nice:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span></div></div>
<p>The main area in which I intended to use this structure was in native PHP templating. In line with this thinking, here is the above restated as I would present it in a template:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>,<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>As you can see, these last three examples are actually quite readable (the last one as much so as native PHP templating can be). The big bonus of course is that they still allow access to all variables in their scope and have no significant performance impact (provided of course that your condition is not intensive on resources).</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fezzatron.com%2F2009%2F07%2F22%2Fforeachif-revisited%2F';
  addthis_title  = 'Revisited%3A+foreachif%28%29';
  addthis_pub    = 'ezzatron';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://ezzatron.com/2009/07/22/foreachif-revisited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP wish list &#8211; part 2: reverse combined operators</title>
		<link>http://ezzatron.com/2008/12/12/php-wish-list-part-2-reverse-combined-operators/</link>
		<comments>http://ezzatron.com/2008/12/12/php-wish-list-part-2-reverse-combined-operators/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 06:38:39 +0000</pubDate>
		<dc:creator>Erin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[wish list]]></category>

		<guid isPermaLink="false">http://ezzatron.com/?p=40</guid>
		<description><![CDATA[It&#8217;s time for part 2 of my PHP wish list! In this instalment I&#8217;ll detail another feature that I would love to see in PHP: reverse combined operators. If you missed part 1 (you probably did), you can read it here. &#8220;What the hell is a reverse combined operator?&#8221; I hear you scream. Well, technically [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s time for part 2 of my <strong>PHP wish list! </strong>In this instalment I&#8217;ll detail another feature that I would love to see in PHP: <strong>reverse combined operators</strong>.</p>
<p>If you missed part 1 (you probably did), you can read it <a title="PHP wish list - part 1: foreachif()" href="http://ezzatron.com/2008/12/04/php-wishlist-part-1-foreachif/">here</a>.</p>
<p><em>&#8220;What the hell is a reverse combined operator?&#8221;</em> I hear you scream. Well, technically they probably don&#8217;t exist yet, so I&#8217;m sure a better name could be found, but bear with me and I&#8217;ll try to explain&#8230;</p>
<p><span id="more-40"></span></p>
<p>If you&#8217;ve ever done any programming, then you&#8217;ve almost certainly used <strong>operators</strong> whether you realise it or not. Basically, they are some of the most basic building blocks of a programming language, things like <strong>+</strong>, <strong>-</strong>, <strong>=</strong> and <strong>==</strong> are all <strong>operators</strong>. For a more detailed description, see the <a title="PHP Manual - Operators" href="http://php.net/operators">PHP manual</a>.</p>
<p>One of the most commonly used operators in any language is the <strong>assignment operator</strong> (=). This operator is used to assign a <strong>value </strong>to a <a href="http://php.net/variables">variable</a>. Here is a simple example:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$variable</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$variable</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs &quot;2&quot;</span></div></div>
<p>In the above example, PHP first uses the <a title="PHP Manual - Arithmetic Operators" href="http://www.php.net/manual/en/language.operators.arithmetic.php">addition operator</a> to get the sum of 1 and 1. It then stores the result in <strong>$variable</strong> using the <strong>assignment operator</strong>.</p>
<p>Now consider the following situation; you have a variable that you want to add a certain amount to. Rather than writing the whole expression out like this:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$variable</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$variable</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></div>
<p>PHP allows you to shorten the above to just:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$variable</span> <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></div>
<p>This is called a <strong>combined operator</strong> because it is effectively a combination of the <strong>+</strong> and <strong>=</strong> operators. This combining of operators is only ever done with the assignment operator and one other operator. Here are all the combined operators that I am aware of:</p>
<table style="text-align: left;" border="0" width="477">
<tbody>
<tr>
<th>Operator</th>
<th>Equivalent to</th>
<th>Description</th>
</tr>
<tr>
<td>$a += $b</td>
<td>$a = $a + $b</td>
<td>Addition / array union</td>
</tr>
<tr>
<td>$a -= $b</td>
<td>$a = $a &#8211; $b</td>
<td>Subtraction</td>
</tr>
<tr>
<td>$a *= $b</td>
<td>$a = $a * $b</td>
<td>Multiplication</td>
</tr>
<tr>
<td>$a /= $b</td>
<td>$a = $a / $b</td>
<td>Division</td>
</tr>
<tr>
<td>$a %= $b</td>
<td>$a = $a % $b</td>
<td>Modulus</td>
</tr>
<tr>
<td>$a .= $b</td>
<td>$a = $a . $b</td>
<td>Concatenation</td>
</tr>
<tr>
<td>$a &amp;= $b</td>
<td>$a = $a &amp; $b</td>
<td>Bitwise and</td>
</tr>
<tr>
<td>$a |= $b</td>
<td>$a = $a | $b</td>
<td>Bitwise or</td>
</tr>
<tr>
<td>$a ^= $b</td>
<td>$a = $a ^ $b</td>
<td>Bitwise xor</td>
</tr>
<tr>
<td>$a &lt;&lt;= $b</td>
<td>$a = $a &lt;&lt; $b</td>
<td>Left shift</td>
</tr>
<tr>
<td>$a &gt;&gt;= $b</td>
<td>$a = $a &gt;&gt; $b</td>
<td>Right shift</td>
</tr>
</tbody>
</table>
<p>What I would like to see are <strong>reverse combined operators</strong>. Essentially these would be the same as regular combined operators, except with the order of operands <strong>reversed</strong>. This would produce the following options:</p>
<table style="text-align: left;" border="0" width="477">
<tbody>
<tr>
<th>Operator</th>
<th>Equivalent to</th>
<th>Description</th>
</tr>
<tr>
<td>$a =+ $b</td>
<td>$a = $b + $a</td>
<td>Array union</td>
</tr>
<tr>
<td>$a =- $b</td>
<td>$a = $b &#8211; $a</td>
<td>Subtraction</td>
</tr>
<tr>
<td>$a =/ $b</td>
<td>$a = $b / $a</td>
<td>Division</td>
</tr>
<tr>
<td>$a =% $b</td>
<td>$a = $b % $a</td>
<td>Modulus</td>
</tr>
<tr>
<td>$a =. $b</td>
<td>$a = $b . $a</td>
<td>Concatenation</td>
</tr>
<tr>
<td>$a =&lt;&lt; $b</td>
<td>$a = $b &lt;&lt; $a</td>
<td>Left shift</td>
</tr>
<tr>
<td>$a =&gt;&gt; $b</td>
<td>$a = $b &gt;&gt; $a</td>
<td>Right shift</td>
</tr>
</tbody>
</table>
<p>Note that not every combined operator would imply the need for a reverse version because the order of operands has no effect on the result in some cases.</p>
<p>I&#8217;ll now provide you with a couple of examples where something like this could come in handy.</p>
<p><strong>Inverting a number</strong>:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$v</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$v</span> <span style="color: #339933;">=/</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// would output &quot;0.01&quot;</span></div></div>
<p><strong>Controlling which array takes precedence</strong> when merging arrays using the <strong>array union operator</strong> (basically just the <strong>+</strong> operator, but when used on arrays you get the union of the two):</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$v</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'one'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'two'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'three'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$v</span> <span style="color: #339933;">+=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'apple'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'orange'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// this would output &quot;one,two,three,orange,&quot;</span><br />
<br />
<span style="color: #000088;">$v</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'one'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'two'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'three'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$v</span> <span style="color: #339933;">=+</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'apple'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'orange'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// this would output &quot;one,apple,banana,orange,&quot;</span></div></div>
<p>I&#8217;m sure if you racked your brain you could come up with dozens of other handy little uses for this feature. Let me know if you can think of any others!</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fezzatron.com%2F2008%2F12%2F12%2Fphp-wish-list-part-2-reverse-combined-operators%2F';
  addthis_title  = 'PHP+wish+list+%26%238211%3B+part+2%3A+reverse+combined+operators';
  addthis_pub    = 'ezzatron';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://ezzatron.com/2008/12/12/php-wish-list-part-2-reverse-combined-operators/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP wish list &#8211; part 1: foreachif()</title>
		<link>http://ezzatron.com/2008/12/04/php-wishlist-part-1-foreachif/</link>
		<comments>http://ezzatron.com/2008/12/04/php-wishlist-part-1-foreachif/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 13:16:30 +0000</pubDate>
		<dc:creator>Erin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[wish list]]></category>

		<guid isPermaLink="false">http://ezzatron.com/?p=3</guid>
		<description><![CDATA[PHP is a great programming language. Honestly, I&#8217;ve worked with it almost every day for 5 or so years and I still love it. I haven&#8217;t even considered trying Python, promise! *looks guiltily at Andi and Zeev* Sometimes though, I do wish for something more. Never enough to switch teams (sorry Python), but there are [...]]]></description>
			<content:encoded><![CDATA[<p><a title="PHP" href="http://php.net/">PHP</a> is a great programming language. Honestly, I&#8217;ve worked with it almost every day for 5 or so years and I still love it. I haven&#8217;t even <em>considered</em> trying <a title="Python" href="http://python.org/">Python</a>, promise! <em>*looks guiltily at Andi and Zeev*</em></p>
<p>Sometimes though, I do wish for something more. Never enough to switch teams (sorry Python), but there are a few points I keep coming back to. This, in fact, is at the present moment the sole reason for this blog&#8217;s existence &#8211; I simply don&#8217;t have anywhere else to voice my frustration.</p>
<p>So, without further ado I give you part 1 of my PHP wish list: the <strong>foreachif() construct!</strong></p>
<p><span id="more-3"></span></p>
<p>The <a title="foreach() contruct" href="http://php.net/foreach">foreach()</a> construct is a feature of many modern programming languages, including PHP. In PHP&#8217;s case, there are two syntaxes:</p>
<pre>foreach (array_expression as $value)
    statement
foreach (array_expression as $key =&gt; $value)
    statement</pre>
<p>Basically it will iterate over every value in an array but it has advantages over a regular <strong>for()</strong> loop in cases where the array keys are non-sequential (and it can also iterate over an object&#8217;s member variables).</p>
<p>Whilst <strong>foreach()</strong> is a very useful construct in itself, I too often find myself wrapping everything inside the foreach() loop in one big <strong>if()</strong> statement:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #666666; font-style: italic;">/* some condition */</span><span style="color: #009900;">&#41;</span><br />
   <span style="color: #009900;">&#123;</span><br />
      <span style="color: #666666; font-style: italic;">// do something</span><br />
   <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now, imagine if there were a new construct formed by combining the above <strong>foreach()</strong> with PHP&#8217;s basic <strong>if()</strong> construct&#8230;</p>
<pre>if (expr)
    statement</pre>
<p>&#8230;to form a new construct, <strong>foreachif()</strong> with the following syntaxes:</p>
<pre>foreachif (array_expression as $value; expr)
    statement
foreachif (array_expression as $key =&gt; $value; expr)
    statement</pre>
<p>Where statement would be executed for each value in <em>array_expression</em> where <em>expr</em>&#8216;s Boolean value equates to TRUE.</p>
<p>The <em>expr</em> part of the syntax would have to be evaluated last so that the logic of <em>expr</em> could involve the use of <em>$value</em> (and <em>$key</em> in the second form).</p>
<p>For example, the following code (which will echo only even number values from an array):</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Could be written much more concisely using <strong>foreachif()</strong>:</p>
<div class="codecolorer-container php vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
foreachif <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Of course some people would call this lazy, but to me it feels natural. In any case, don&#8217;t hold your breath, due to a clear lack of any real <em>need</em> for this feature it&#8217;s doubtful this will ever make into <a title="Taking a look at PHP 6" href="http://jero.net/articles/php6">PHP 6</a>. Perhaps <a title="PHP 10.0 Blog" href="http://php100.wordpress.com/">PHP 10</a>?</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fezzatron.com%2F2008%2F12%2F04%2Fphp-wishlist-part-1-foreachif%2F';
  addthis_title  = 'PHP+wish+list+%26%238211%3B+part+1%3A+foreachif%28%29';
  addthis_pub    = 'ezzatron';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://ezzatron.com/2008/12/04/php-wishlist-part-1-foreachif/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
