“GOTO” in PHP?

Tagged: PHP Date: 12th, August 2008

I have just been reading up meeting notes from PHP Dev team and I found few very interesting conclusions. One of them regards introducing GOTO in PHP (remember GOTO 10?).

There has been a lot of discussion on GOTO statements during the years most of them citing “Goto considered harmful” [uri]. Even with all those comments and articles on GOTO I still haven’t heard argument against it that doesn’t resolve to “it looks ugly and breaks the flow” at the end.

Sure, there will be a lot of beginners that will use it wrongly… but it is still very useful when breaking out of a nested loop or nested if statements. If you ask me I am all for it.

Now a bit about implementation… we will probably see reuse of break keyword instead of goto. Reason for this is that a possible php GOTO is not a real GOTO construct. Break keyword would be extended with “static label”:
Cite:

for ($i = 0; $i < 9; $i++)
{
        if (true) {
                break blah;
        }
        echo "not shown";
blah:
        echo "iteration $i\n";
}

One of limitations that was discussed (to stop misuse) was restricting the construct so that you can only jump out of a construct is possible... (one example of misuse would be jumping to beginning of foreach() loop).

Another interesting thing was that ifsetor will probably not be introduced.

Full php meeting notes (uri)

Leave a Reply