///GOTO is coming to PHP!

GOTO is coming to PHP!

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:

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";
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.

2008-01-28T01:03:37+00:00 January 28th, 2008|PHP|2 Comments

About the Author:


  1. Anonymous February 28, 2009 at 2:10 PM


    They horribly neutered a perfectly good and long-needed goto command in the earlier betas into this piece of crap. As you say, it’s a labeled break. Sure, labeled breaks are good, but you can’t POSSIBLY call this a goto. PHP needs a REAL goto, ffs.

    I’d sooner write my own webserver than use this bullshit crap.

  2. Frank Rizzo August 22, 2009 at 8:41 PM

    You should stop coding if you really think that PHP needs a goto.

Leave A Comment