by Akash Mehta
|
|
GOTO, the infamous much-loathed feature of languages in decades gone by, is (sort of) coming to PHP. Read on to see why it’s actually a good thing.
Luckily, it’s not that bad. In fact, it’s quite handy.
Take a look at the following scenario:
while($var)
{
foreach ($var as $val)
{
if ($val) break 2;
}
}
Now, consider all this is inside another while loop. If someone else comes in and refactors, taking out the foreach loop, your ‘break 2;’ suddenly not only breaks out of the while($var) but also the loop outside of it. Big trouble.
Of course, GOTO is associated with Bad Things. A GOTO of sorts is coming to PHP 6, and is actually a good thing. It’s not so much a GOTO as a named break - it allows you to use your break statements to go to a particular point in code execution, as opposed to all this messy nested looping and loop numbering. Here’s a sample:
for ($i = 0; $i < 9; $i++)
{
if (true) {
break blah;
}
echo "not shown";
blah:
echo "iteration $i\n";
}
?>
I believe it was discussed at a meeting some time back; see here for the meeting notes. In any case, it’s a useful addition, provided it isn’t misused; hopefully we won’t end up with the spaghetti code messes of years gone by.
Tags: PHP, php 6
Akash Mehta in PHP |
on Monday, January 28th, 2008 at 1:03 am.
RSS 2.0 |
Leave a response | Trackback