php[architect] logo

Want to check out an issue? Sign up to receive a special offer.

GOTO in PHP 5.3: is it Really That Evil?

Posted by on June 11, 2009

Unless you’ve called the underside of a rock home for the last several days, you have probably noticed that the web at large has
discovered that the infamous GOTO statement will make its debut in the upcoming version 5.3 of the PHP language.

Unsurprisingly, developers from many walks of life have sounded off on this latest addition to the language—mostly with negative comments (and we use “negative” in a very restrained sense here), tied to the traditional view that GOTO is indicative of poor programming skills—a view that was originally popularized by Dutch computer pioneer E. W. Dijkstra.

Why a GOTO statement?

A number of traditional and lower-level languages, including C,implement GOTO statements, or an equivalent construct. Typically, however, higher-level languages eschew GOTO in favour of more complex control structures that give code a better-defined structure. As such, GOTO has out of favour with proponents of best practices, primarily because it makes the analysis of code flow difficult.

The question then is, why is there a GOTO statement at all? The answer is actually quite simple: it makes writing some code easier. For example, finite-state machines, such as regular expression compilers and compile compilers (that is, compilers that are used to generate other compilers) make extensive use of GOTO statements in a variety of

You may, of course, wonder whether any sane person would implement a compiler in PHP—and you may be surprised. A number of organizations have specialized compilers that are used internally for various tasks; for example, at MTA we use a custom-built compiler to process and manipulate manuscripts written using our proprietary Ceres format. The compiler is written in PHP for portability and ease of installation and, although it was created by a compiler compiler also written in PHP without the help of GOTO statements, it took much more work than would have otherwise been possible had GOTO been available.


To understand how GOTO made it into PHP, we need to go all the way back to 2006, when Sara Golemon introduced it into the then-current 6.0 branch of the language. It took several months of discussion before the construct actually made it “officially” into PHP, however—as she announced in this post on her blog in March of that year.

I caught up with Sara via IRC yesterday, and asked her what motivated her to introduce GOTO (and the inevitable controversy) into PHP. Predictably, she responded that she was simply scratching an itch:

[T]he project I was working on (and may pick back up someday) was a workflow based site builder […] for making dynamic websites without knowing a programming language. You build up all the meta-information about how your site should run, then the site-builder “compiles” that into PHP code that’ll run on the actual server.

It can be done without GOTO, but the resulting logic for the particularly complex bits of business logic become much simpler with it.


As you can see, GOTO made it into PHP the way most new features do: because someone needed it. This hasn’t stopped developers from making a series of negative comments (in some case, unfortunately so) about whether the construct should be part of the language—not only in the manual page linked above, but even in a post on Reddit, ironically a full three years after GOTO was first committed to PHP’s HEAD.

Out of the several opinions expressed about GOTO, however, I came across that of Timothy Boronczyk, author of several articles for php|architect, who not only suggests that all the GOTO hate is
unfounded, but also proceed to show a few practical examples in which GOTO is actually helpful on his blog.

“I think GOTO is a deus-ex-machina for programmers. Most of the time it’s like shouting ‘Help! I’ve coded myself into a corner and can’t get out,’” he said in an interview via e-mail, adding that “if the average programmer plans things out appropriately at the beginning, he won’t find himself in a spot to even needs to consider using GOTO. My blog post was a sharing exercise, trying to put the fervent anti-GOTO sentiments we’ve had drilled into us aside for a moment and consider whether there were any legitimate
uses for GOTO.”

Effects of GOTO

Timothy (as well as several others that I have spoken with) thinks that the introduction of GOTO will, for the most part, be completely harmless to the average developer:

GOTO is no different than an if or switch statement. The absolute beginner has more trouble wrapping his mind around while-loops because suddenly there’s a conditional and variable states to keep track of, or understanding PHP’s internal typecasting rules.

GOTO will affect those who let themselves be affected by GOTO. I don’t mean that to sound like a non-answer… but some will work themselves up into frenzy and use it as fodder to bash PHP, others will take the approach I did and explore whether they can make use of a new language feature, and the rest will probably just ignore it. GOTO is simply another option PHP makes available to the programmer, just like functions, objects, variable-variables, and so on. It’s up to the programmer to know which constructs are suitable for the task at hand.

One thing is certain: there are some application in which a good developer can put GOTO to good use to produce code that is simpler and more efficient, but they are probably not the kind of mainstream programs that your average developer will be writing; therefore, there is a legitimate concern here that inexperienced programmers will use GOTO to the general detriment of their code.

On the other hand, there are a number of PHP features for which the same could be said—and GOTO is yet another example of what makes PHP the “language of people who want to get things done.”


What do you think? Is GOTO a good addition to the PHP language, or will it cause more headaches for developers? Sound off in our comments below

Edit (2009-06-12): fixed some typos.

Marco is the keeper of keys and Chief Garbage Collector at Blue Parabola. He can be found on Twitter as @mtabini.
Tags: , ,

Responses and Pingbacks

GOTO is a ‘tool’ like a computer or any other device – used in the right way it can be helpfull – used in the wrong way it may not. So if you can teach good programming habits you don’t need to bann tools.

Non conditional jumps have been a part of computer engineering from day one, so if someone can prove that using of JMP in asm is not good, I might than believe him that GOTO is something we should burn with fire.

Use it wisely! 😉

Disagreement on
May 2nd, 2012 at 2:16 am

GOTOs can be extremely useful when used sparingly. Everyone creates massive bloated code with function call after function call, storing stacks, unloading stacks, its all ridiculous! Coming from a micro-controller programmer standpoint, GOTOs, used effectively, can reduce SIGNIFICANT overhead in programs. I know what everyone is thinking who is an APP developer for PC’s “I have 3GHz and 8GB of ram at my disposable, why do I care?”. Well that non-caring attitude is the same reason that drives more and more people to code inefficiently, and before you know it, you wind up with Microsoft Office or some equally bloated garbage.

For god sakes, use GOTOs if it makes sence to use them. I commend PHP for adding this addition, just to spit in the face of all these “abstract architectural type programmers”. You know, the “single entry singl exit” bullshit. How retarded are you that you cant follow returns and breaks? Maybe you should stop criticizing everyone else’s code and start analyzing your programming skills.

Great post.

Leave a comment

Use the form below to leave a comment: