If PHP then goto is the future

If PHP then goto is the future

Summary: Few things can spark more religious fervour amongst programmers than the mention of a goto statement.


Few things can spark more religious fervour amongst programmers than the mention of a goto statement. PHP has stepped into the middle of the firefight by announcing that it will be implementing goto functionality in version 5.3.

There is plenty of discussion surrounding PHP's decision, with PHP founder Rasmus Lerdorf defending the move, stating it is no different than breaking to a label and that it will not be an unlimited goto (it will not let jumps into loops or switches).

Despite Lerdorf's assurances, the wailing and moaning continues relentlessly. Why is this? To understand, one must realise that the vast majority of programmers have been taught that goto is considered harmful, is a bad idea and will bring pestilence to the land if used. Most students are happy to accept this, generally based on the experience of writing ridiculous spaghetti code in BASIC, so they move onto structured programming and eventually object-orientated programming.

This happens for very good reasons; a maturing programmer has more chance to get themselves in a bind by being unstructured and it is generally better for maintainability and understanding to avoid goto.

By going against the goto grain, PHP is challenging a tenet upon which the foundation of modern programming is built. The size of the challenge is only compounded by the fact that the change is occurring in version 5.3 of the language, a long time after the language muffed but eventually reached satisfactory attempts at implementing objects, and appears to be a step backward.

xkcd says it best on bad uses of goto (Click image to enlarge)
(Goto image by xkcd, CC2.5)

Here's a shock for the young, blindly-accepting-articles-of-faith type of programmer that has never had to use a goto: most high-level languages implement a form of goto, you just know them as break, switch and continue. And that most-revered of code projects, the Linux kernel, makes extensive use of the goto statement and is written in C — the most respected of languages.

What is good enough for Linus and the team of kernel hackers should be good enough for Rasmus and his clan of web developers.

It's true that goto is a dangerous weapon and in the hands of the inexperienced it's a devastatingly head-scratching weapon of spaghetti creation, but in the right hands it can remove the maze of nested branching structures nicely.

What does this all mean for PHP? Not much. Those programmers adverse to using goto will continue to avoid it at all costs (no one is forcing them to use it), quality programmers will be able to use it well and sparingly, and the bad programmers that create spaghetti will do so even more now that they can use the statement.

But they were going to create bad programs anyway, goto simply made it easier for them to get into a mess. They'll learn eventually.

Topics: Open Source, Linux, Software Development


Chris started his journalistic adventure in 2006 as the Editor of Builder AU after originally joining CBS as a programmer. After a Canadian sojourn, he returned in 2011 as the Editor of TechRepublic Australia, and is now the Australian Editor of ZDNet.

Kick off your day with ZDNet's daily email newsletter. It's the freshest tech news and opinion, served hot. Get it.


Log in or register to join the discussion
  • Python PEP 315

    What are you saying about my BASIC code?

    But seriously, I noticed recently that there was a proposal put forward for the python language recently (PEP 315) to introduce a post test loop. Part of the justification for the proposal was that to implement such code at present required an infinite loop with a break or continue statement. The writer of the proposal argued that this was akin to the use of goto and thus it was bad practice to require their use. The proposal was defeated; it would appear because none of the suggestions were consistent enough with existing python syntax.

    It's certainly an interesting question though: are there commands that are just too powerful? Though functionally similar goto is more powerful than break, continue or switch. Having taught myself to program BASIC at age 12, I can attest to the problems you can get into using it, but there are times when it will be the most efficient method.
  • What's the reason behind?

    So if there is goto in C, and Linus made Linux Kernel using goto a lot, does it mean goto is good? I would like to know the real motive of Rasmus that compelled him to introduce this into PHP.
  • GOTO is horrible to maintain

    This is the bit that really is the crux of the matter:

    "quality programmers will be able to use it well and sparingly, and the bad programmers that create spaghetti will do so even more now that they can use the statement."

    If you inherit a goto ridden mess to maintain, then having the statement in the language will matter. I just think it unnecessary to have it. Either way, I don't think it's the end of the world if we the language incorporates it.
  • goto != break, continue...

    Break and Continue statements are structured in a manner that you can only use them in a certain context. The problem with goto statements isn't that their functionality is a bad idea, it's that it is way to ambiguous and creates headaches in terms of readability, maintainability, and other aspects of pragmatic programming. There are plenty of other control statements in numerous language that take the execution of the code from one point to another. The fact of the matter is that the goto statement should not have to be used and for easy to entry languages like PHP it's seems like the justification for having it there is to make it easier for programmers to avoid learning the correct way to write the program.
  • Bad Comparisons

    It isn't quite accurate to equate goto with break and continue. break and continue have very specific constraints that prevent them from leading to the same level of insanity as goto.

    Using C and the Linux Kernel as a reason to put goto into PHP? That's a bit ridiculous. C has pointers, maybe we should get pointers in PHP then? Maybe someone should write a kernel in PHP? Please note the sarcasm :)

    Truth is, I don't really buy any of the explanations I've heard for adding goto to PHP... I think it does FAR more damage than good.
  • argument dead since 1968

    Dijkstra killed this argument in 1968. Why is this coming up again? Did php decide it no longer wants to be taken seriously?
  • goto

    one would hope that the xkcd site in the blurb was not an active one, as my security suite responded, as though it was. "High Risk" it said
  • They're all very unstructured

    I don't see GOTO's anymore, but I still shudder when I see break, continue or (most particularly) exit. They're better. When you see a GOTO, you have no idea where it's going to until you hunt for and find the target label. With the others you know exactly where they're going.

    BUT when you're looking at the end of a loop or method, you have no way of knowing what condition brought you there if there are breaks or exits in the body.

    Break and exit violate the principle of SESE just as much as a goto. They represent a maintenance risk. I've never encountered a situation where break, continue or exit couldn't be coded clearly, concisely and efficiently.
  • Goto in C and Linux because Hardware

    C and Linux use the goto for specific situations that need to match the hardware for jumpers and so on but PHP a really higher level language used for Web Application and Website development? hilarious, PHP continue to impress me in the retard programming language space. ROFL.

    Also the new design of the namespace really blows.
  • PHP Object Oriented Programming Language


    My name is Haim and I currently lecture a PHP course... covered in my blog at http://www.lifemichael.com/en/?p=548 where me and my students refer to your post.

    My origins in Java. I develop Java applications for more than thirteen years years. Three years ago I chose to delved into PHP.

    Reading your post I find it highly important to mention that PHP is currently an OOP language allowing to create simple, clean and maintainable code (as in Java). Whether 'goto' will get its support... or not... good programmers who know OOP properly won't even put the effort to learn the PHP new 'goto' command.

    Regarding the usage of 'goto' in other languages... I want to specify those other languages are very old ones... without any object oriented capabilities.



  • Don't bother

    Don't bother to explain. I doubt Chris Duckett knows much about programming :)
  • Why not?

    To break out just hold down RUN/STOP Restore?
  • Goto

    Having a GOTO will just make it easier to translate BASIC or any other language with GOTO in it to PHP.

    The more functionality a language has the more ability it has to have code examples transcoded.
  • Linus Torvalds on the Importance of Annoying Programmers


    Maybe the heavy use of goto's in the Linux Kernel serves the same purpose.
  • deny goto!

    goto is not the same as break, continue or switch! And this is not the only fact that compromises the author's competence.
    There are maths based reasons to avoid goto, why don't they implement division by zero then? So many zero-durability checks will be made unnecessary.