Is GOTO in PHP evil?

46,568

Solution 1

Unless you are programming in assembler, GOTO should always be treated the same way as the life vest of the airplanes: it is good to have them available, but if you need to use them it means that you are in big trouble.

Solution 2

I can't believe nobody posted this :)

xkcd - goto

Granted, PHP is not compiled... Maybe the raptor will chase you on every visit to your website?

Solution 3

Bad structuring of code is evil, regardless the control structure you use.

I personally prefer a goto that makes clear the flow of the program to "control variables" and nested "if" that will indirectly just cause the same branch in the code.

So, just write the two versions (with and without GOTO) and see which one it's easier to comprehend. Then the choice is easy.

Solution 4

I think this is the most important part of the PHP manual page and missing here:

This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure. You may jump out of these, and a common use is to use a goto in place of a multi-level break.

IMHO this makes it very different from the ye olde BASIC style gotos.

Solution 5

I'm in the minority (currently), but I believe the restrictions placed on PHP's goto construct make a very beneficial tool:

http://adamjonrichardson.com/2012/02/06/long-live-the-goto-statement/

I actually walk through an example of arrow code (deeply nested conditionals) and refactor it using standard practices (guard clauses, grouping conditions, pulling out functions) in one version and a goto-based version in the other version, and I actually prefer the goto-based refactoring.

Share:
46,568
Tower
Author by

Tower

Updated on July 05, 2022

Comments

  • Tower
    Tower 6 months

    I recently found out that PHP 5.3 supports new language construct called GOTO. Everybody knows what it does. However, it's not exactly the traditional GOTO, it's just a jump label. I'm interesting in knowing whether this GOTO is evil and implies bad code?

  • Alex Budovski
    Alex Budovski about 13 years
    Guns don't kill people. Magic missiles do.
  • Carson Myers
    Carson Myers about 13 years
    I'm scared to use goto because anyone who reads my code might completely lose faith in me
  • Alnitak
    Alnitak about 13 years
    I haven't used a goto (except in assembly) for nearly 20 years. Not even when I was writing (DEC) Fortran.
  • Remo.D
    Remo.D about 13 years
    I would rather say that "if you need to use them AND YOU DON'T HAVE them, you are in big trouble". Which is a strong point in favour of having goto into a programming language.
  • danii
    danii about 13 years
    The funniest bit is that his comic is on the php manual reference for goto: es.php.net/manual/en/control-structures.goto.php so yes, the raptor WILL chase you, even in PHP ;) also great alt text: "Neal Stephenson thinks it's cute to name his labels 'dengo'"
  • TheHippo
    TheHippo about 13 years
    I would give you +2 if I could (one for being right, one for the comic)
  • MattBianco
    MattBianco over 12 years
    +1 for the first two paragraphs. Why isn't this the accepted answer instead? :-)
  • Alastair
    Alastair almost 10 years
    Well...it's reasonable but we all know, deep down, we came to this page because we felt guilty about thinking of using goto x;. It's never someone's ideal workflow, just life-support that we know we'll never re-write, instead, we'll sadly leave for the next poor guy to deal with.
  • Buttle Butkus
    Buttle Butkus over 9 years
    @Remo.D Yes, because life vests have saved so many lives in plane crashes!
  • Remo.D
    Remo.D over 9 years
    @ButtleButkus Not in plane crashes but they saved many lives in emergency landing on water (sea, lakes, rivers).
  • EThaiZone over 9 years
    I use goto in this micro framework that I wrote. CLIP Framework for cli. It's correct that goto can't use across files but you can use it on 1 file and use this as class or function to control programming flow. I hope this can help people to think alternative. PHP is more than a web programming.
  • Anthony Scaife over 8 years
    +1 I use it to jump to the end of inline code, to avoid ugly nested if()'s. It can also avoid unnecessary code execution - eg code you were executing before an if-elseif-elseif-else block where it could have "bailed out" after the 2nd elseif.
  • Anthony Scaife over 8 years
    If it is not available in your version of PHP, then a "break;" in the middle of a do{ ... } while(0) may be appropriate to you. I used to do this (with documentation naturally).
  • Peter almost 8 years
    Actually, I did have one scenario where goto literally saved us. We started using an MVC, and we were using the MVC for dependency management since it had a very mature class loading system. Well, all of a sudden, in legacy files that used the traditional header(); die; construct, APC started throwing segmentation faults at random. Goto became the life vest that kept that legacy code floating until it could all be refactored into controllers. It also offered improved performance by avoiding overwhelmingly deep conditionals
  • xZero
    xZero almost 8 years
    Very well explained. I really like this answer. :D
  • DeepBlue
    DeepBlue about 7 years
    Screw structure let's use GOTO for the fun of it !!
  • Nick Rice
    Nick Rice over 6 years
    My car lets me drive at 90 down the high street. That doesn't make it evil. If it made me drive at 90, then it would be. Driving at 90 down the high street would be evil, not being provided with the ability to do so.
  • Ignacio Soler Garcia
    Ignacio Soler Garcia over 6 years
    If your car lets you drive at 90 when there is no need to go at 90 ever then your car is evil' ish as it give you some capabilities that only can hurt you.
  • Mike Q
    Mike Q over 6 years
    I've never used a GOTO or even a continue, unless in a crappy Windows Bat file to call functions. Explain why you needed it please.
  • TecBrat
    TecBrat over 6 years
    @IgnacioSolerGarcia, If the highest speed limit in my area is 70, and everyone is driving 70 and I need to accelerate to avoid a crash, I sincerely hope my car does not top out at 70. So, it's still a matter of how it's used. Guns, fast cars and GOTOs...
  • Ignacio Soler Garcia
    Ignacio Soler Garcia over 6 years
    @TecBrat: I think that the answer specifies clearly that it not a fixed rule. Until know I never needed to accelerate over the top speed to avoid a crash, maybe some day I will need to do it once, but for sure is something that you don't need every week or every month
  • EThaiZone over 6 years
    @MikeQ Sometimes I need to write daemon software to do some background job. It must monitor input from many sources and do tasks that I assign it. For daemon process, many people will write with while(true) so it can do forever until you kill it but this is just dirty way to do it.
  • EThaiZone over 6 years
    Someone may think GOTO will break login flow because this command just can skip sequence in your code but I think this problem cause by developer does not fail from language. Even if you have better language, you can make it crash with simple while(). If you don't use GOTO that don't mean it should remove from language.
  • Trent
    Trent over 5 years
    This is such an old post - but I have to agree here... It's a control structure like any other and if you're a shit coder, you will write shit control structures with or without goto. I've seen code embedded with and nested 5/6 levels deep with control variables if ($somePass): when really each pass is just checking to see it has what it needs, and wants to go somewhere else to finish the work.. Go GOTO you good little thing you!
  • antoineMoPa
    antoineMoPa about 5 years
    In Canada, we have less guns and less gun violence. I therefore recommend not having goto statements.
  • userfuser
    userfuser almost 5 years
    @antoineMoPa so, don't use goto in Canada.
  • userlond
    userlond almost 5 years
    The raptor will chase you on every visit to your website via non-Mozilla browser :)
  • That Realty Programmer Guy
    That Realty Programmer Guy over 4 years
    obviously the goto are virtuous. we just need licensed people who have been through a goto safety course
  • Roshimon over 4 years
    I use goto and it really helps me in avoiding unnecessary control structures. I can keep the code short as well as readable. I am happy that we have this in Php.
  • thephpdev
    thephpdev over 4 years
    Gotta love when stackoverflowers argue over the semantics of an analogy used to make a point...
  • UncaAlby
    UncaAlby over 3 years
    @antoineMoPa at the risk of my getting banned from SO, this comment is like saying the Republic of Congo has fewer deaths caused by automobiles because they have fewer automobiles. Automobiles, as well as goto statements, have a proven utility in certain situations. So perhaps we can require a "Goto License"?
  • UncaAlby
    UncaAlby over 3 years
    Bad programmers can write bad programs no matter what. If we forbade any syntax that allows bad programmers to write bad programs, there would be nothing remaining to write anything in.
  • Luke over 3 years
    Thank you - loved it in BASIC. Avoid it in PHP - but I find it easier to manage when it's restricted to the function or file. True PHP manual doesn't explain it very well.
  • schmunk
    schmunk over 3 years
    This is a pretty cool example for goto in PHP btw github.com/schmunk42/retry/blob/master/src/retry.php - credits go to igorw
  • Yay295
    Yay295 over 3 years
    ... use a goto in place of a multi-level break. is an interesting statement considering PHP does actually have a multi-level break. break 2; will break out of two loops for example. Just leaving this here for anyone who comes along; a multi-level break might be all you need.
  • ericek111
    ericek111 almost 2 years
    @schmunk, that's exactly why I looked up this topic. Though instead of using goto for the first time in my life, I'll probably go with while.