PHP wish list – part 1: foreachif()
PHP is a great programming language. Honestly, I’ve worked with it almost every day for 5 or so years and I still love it. I haven’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 a few points I keep coming back to. This, in fact, is at the present moment the sole reason for this blog’s existence – I simply don’t have anywhere else to voice my frustration.
So, without further ado I give you part 1 of my PHP wish list: the foreachif() construct!
The foreach() construct is a feature of many modern programming languages, including PHP. In PHP’s case, there are two syntaxes:
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
Basically it will iterate over every value in an array but it has advantages over a regular for() loop in cases where the array keys are non-sequential (and it can also iterate over an object’s member variables).
Whilst foreach() is a very useful construct in itself, I too often find myself wrapping everything inside the foreach() loop in one big if() statement:
{
if (/* some condition */)
{
// do something
}
}
Now, imagine if there were a new construct formed by combining the above foreach() with PHP’s basic if() construct…
if (expr)
statement
…to form a new construct, foreachif() with the following syntaxes:
foreachif (array_expression as $value; expr)
statement
foreachif (array_expression as $key => $value; expr)
statement
Where statement would be executed for each value in array_expression where expr‘s Boolean value equates to TRUE.
The expr part of the syntax would have to be evaluated last so that the logic of expr could involve the use of $value (and $key in the second form).
For example, the following code (which will echo only even number VALUES
from an array):
Could be written much more concisely using foreachif():
Of course some people would call this lazy, but to me it feels natural. In any case, don’t hold your breath, due to a clear lack of any real need for this feature it’s doubtful this will ever make into PHP 6. Perhaps PHP 10?
16 Comments »
RSS feed for comments on this post. TrackBack URL
Nah – switch to Python… make your life easier :p
First!
EPIC FAIL!
Damnit!
this could quite possibly be the nerdiest blog i have ever seen…and i love it. The coming together of two things that deserve to walk together in cyber space…nice work. I am going to read and not understand a thing.
Cheers dude! It’s good to have place to air my nerdy laundry. On the other hand, I’m working on some less nerdy stuff right now, so the next one might disappoint a little.
I’m surprised you didn’t go as far as suggest a “foreachelse”?
I already think that PHP suffers from an overbloat of functions and codes, and actually could do with more simplification instead of worrying about having to write:
if (is_array($x)) { foreach ($x => $stuff) { … } }
Yes, an else clause on foreach() statements would be nice too, and I did consider adding it to this article.
In the end I decided to leave it out and focus on one thing at a time. Perhaps I will write another PHP wish list article to cover that.
[...] If you missed part 1 (you probably did), you can read it here. [...]
thinking about it… I think that if PHP simply stopped going gaga over an empty array or non-array being used in the foreach, that would be sufficient…
Ah! Now I understand your last comment better. Actually, I think you may have the wrong idea about what I meant by foreachif.
The idea is NOT to skip the whole construct when the expression returns false, but to evaluate the expression every time the loop iterates, and skip ONLY that single iteration if the result is false.
Either that or I’m not picking up what you’re putting down
heh – probably should have read your examples… similar arguments on complexity and readability there – having the condition inline with the foreach does not make the script clear.
But do you agree on my comment about PHP errors on foreach’ing an empty array?
Yes and no…
On the one hand it would be nice if PHP didn’t force you to check existence AND type before using a variable in a foreach() loop, on the other I’d hate PHP to suffer from the same situation as JavaScript where things just fail silently and bugs can easily slip through without being noticed.
Perhaps if is_array() and other variable type-checking functions could handle undefined variables? Hmmm… sounds like another wish list item
You could always build your own foreachif function that accepts and evaluates a string.
The whole thing seems like an exercise in unintentional obfuscation to me, though.
You certainly have a valid point, the last thing I would want is for PHP to end up like Perl.
Looking back at this article I believe I wanted this functionality because I had just started using PHP as a templating system instead of Smarty, and I was missing some of its handy functionality somewhat.
In regards to rolling your own with eval()’d string, yes that could work, but could make debugging more difficult and completely voids any special features provided by your IDE.
A more elegant solution could be to use PHP > 5.3 with the new closures feature ( http://php.net/closures ), although it will be a while before IDEs know how to handle them. Perhaps I’ll write a quick post to demonstrate…
[...] 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 [...]