77

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?

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
Tower
  • 98,741
  • 129
  • 357
  • 507
  • see Question http://stackoverflow.com/questions/723324/php-and-the-goto-statement-to-be-added-in-php-5-3 – Dan Soap Dec 14 '09 at 10:08
  • 2
    GOTO is like regexp for HTML parsing; http://www.codinghorror.com/blog/archives/001311.html – Rubens Farias Dec 14 '09 at 10:08
  • 5
    in college i always used to use goto statements when i wanted to piss off my professors, muahahaha....the programs would of course still work perfectly but i could always see their eyes twitching when they'd come across those little statements – erik Dec 14 '09 at 10:18
  • 16
    >"However, it's not exactly the traditional `GOTO`, it's just a jump label." How does this differ from the "traditional" `GOTO`? – Anon. Dec 14 '09 at 10:20
  • 3
    Recurring `GOTO` questions on StackOverflow considered harmful. – rsenna Aug 27 '13 at 23:40
  • 1
    Sometimes your code is much clearer and robust using goto. For instance some type of parsers or state-machines are best programmed using a goto. But in general, I would avoid it. – Patrick Savalle Nov 19 '14 at 12:15
  • 5
    goto isn't evil at all.. but this is truly evil: `eval("foo: goto foo;");` – CodeBrauer Apr 07 '15 at 13:45
  • if you think goto is evil and you can't use goto safely, cleanly and sanely you're just a bad programmer that's all – That Realty Programmer Guy Apr 03 '18 at 15:52
  • php's goto is pretty limited in capability. It cannot be used to jump over functions or method or loop structures. Although to be fair, there is no such thing as function/method and loop structures in assembly. – Xwtek Oct 29 '20 at 15:08

11 Answers11

158

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.

Konamiman
  • 49,681
  • 17
  • 108
  • 138
  • 66
    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. – Remo.D Dec 14 '09 at 10:25
  • 21
    @Remo.D Yes, because life vests have saved so many lives in plane crashes! – Buttle Butkus May 04 '13 at 06:52
  • 14
    @ButtleButkus Not in plane *crashes* but they saved many lives in *emergency landing* on water (sea, lakes, rivers). – Remo.D May 04 '13 at 09:34
  • 4
    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 – Peter Feb 11 '15 at 13:38
  • Very well explained. I really like this answer. :D – xZero Mar 21 '15 at 14:14
  • Gotta love when stackoverflowers argue over the semantics of an analogy used to make a point... – thephpdev Apr 25 '18 at 10:54
104

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?

Community
  • 1
  • 1
Loris
  • 1,940
  • 3
  • 19
  • 27
  • 20
    The funniest bit is that his comic is on the php manual reference for goto: http://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'" – danii Dec 14 '09 at 11:01
  • 2
    I would give you +2 if I could (one for being right, one for the comic) – TheHippo Dec 14 '09 at 11:12
  • 1
    The raptor will chase you on every visit to your website via non-Mozilla browser :) – userlond Mar 16 '18 at 05:50
86

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.

the
  • 21,007
  • 11
  • 68
  • 101
Remo.D
  • 16,122
  • 6
  • 43
  • 74
  • 6
    +1 for the first two paragraphs. Why isn't this the accepted answer instead? :-) – MattBianco Aug 19 '10 at 14:35
  • 2
    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. – Alastair Feb 06 '13 at 16:46
  • 2
    +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 Jul 25 '14 at 10:25
  • 1
    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). – Anthony Scaife Jul 28 '14 at 11:10
  • Screw structure let's use GOTO for the fun of it !! – DeepBlue Oct 19 '15 at 21:10
  • 1
    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! – Trent Apr 05 '17 at 02:03
  • 1
    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. – Roshimon Apr 19 '18 at 12:55
28

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.

schmunk
  • 4,708
  • 1
  • 27
  • 50
  • 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. – Luke Aug 29 '19 at 04:50
  • This is a pretty cool example for `goto` in PHP btw https://github.com/schmunk42/retry/blob/master/src/retry.php - credits go to *igorw* – schmunk Sep 03 '19 at 06:43
  • `... 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. – Yay295 Sep 10 '19 at 03:56
  • @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`. – ericek111 Mar 02 '21 at 11:54
18

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.

AdamJonR
  • 1
  • 1
  • 2
11

Are guns evil? Both can be used for good or for evil. I would say it was easier to write good code without goto, than with.

Carson Myers
  • 37,678
  • 39
  • 126
  • 176
David Waters
  • 11,979
  • 7
  • 41
  • 76
  • 24
    Guns don't kill people. Magic missiles do. – Alex Budovski Dec 14 '09 at 10:08
  • 2
    In Canada, we have less guns and less gun violence. I therefore recommend not having goto statements. – antoineMoPa Dec 15 '17 at 15:46
  • 4
    @antoineMoPa so, don't use goto in Canada. – userfuser Jan 26 '18 at 11:16
  • 1
    obviously the goto are virtuous. we just need licensed people who have been through a goto safety course – That Realty Programmer Guy Apr 03 '18 at 15:58
  • @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 May 20 '19 at 19:38
  • In Switzerland we have about 1/4 as many guns per capita as the US and we have near zero gun violence. The problem is not the gotos per se, It's how you use them. Which boils down to your level of training and discipline. And yes, we do here require a license for guns. – Dom Jun 16 '23 at 13:28
7

Any language feature that can make code more readable in a given situation is A Good Thing. GOTO is one such language feature, even if those situations are few and far between. If we forbade any syntax that made it possible for poor programmers to write bad, unmaintainable code our jobs would be an awful lot harder.

Mark Rendle
  • 9,274
  • 1
  • 32
  • 58
  • 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. – UncaAlby May 20 '19 at 19:43
5

As a software engineer, i mostly work on "mainframes" and "big corporate servers"... And our daily language (I mean the one in 95% of our base code) is Cobol, which uses extensively GOTOs.

This usage doesn't mean the code is bad. It just means that this tool (GOTO) was the right one at the moment programs were written.

To answer Kaitsuli's question, I think it can be useful tool when writing PHP scripts. On the other hand, a lot of scripts were achieved without it for almost a decade by now. Furthermore, it goes against PHP's evolution with more object-oriented features.

IMHO, it's nor good nor a bad thing for the code be produced : good programs will still be good and "horror programs" will be worse... The only question is : "Why adding GOTOs 10 years after proving it was not necessary ?".

Arno
  • 745
  • 4
  • 17
4

GOTO usually is evil because it lets you build unstructured code. With the usual loops you can build good structured code easy to follow because it is structured.

When you have non structured code jumping from here to there, you have just found the evil coming from the GOTO statement. Almost always is better to avoid it. Maybe once every 100.000 lines there is a place where a GOTO sentence simplifies A LOT the code thus is not evil but if you are unsure, then you should avoid the GOTO.

Hope this helps.

EDIT: Well, just to add my own opinion here, there are other instructions that allow you to create unstructured code and that are not considered evil when I think they should be.

For example a return in middle of a function is a GOTO to the end of it so I avoid them and use only one return in each function just at its end.

Other languages like Vb.Net (maybe others too) allow to do Exit For, Exit While, breaks and things like these that also unstructure the code and I think should be avoid.

FelisPhasma
  • 332
  • 5
  • 13
Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • 9
    I'm scared to use goto because anyone who reads my code might completely lose faith in me – Carson Myers Dec 14 '09 at 10:14
  • I haven't used a goto (except in assembly) for nearly 20 years. Not even when I was writing (DEC) Fortran. – Alnitak Dec 14 '09 at 10:21
  • 1
    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. – Nick Rice Apr 28 '16 at 15:26
  • 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. – Ignacio Soler Garcia Apr 29 '16 at 07:46
  • @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... – TecBrat Aug 04 '16 at 18:15
  • @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 – Ignacio Soler Garcia Aug 16 '16 at 08:57
2

sometimes (I mean in 0.01% of cases) it is useful, like when you have a long long script and you want to test some blocks. but never keep it in your final script

yafrani
  • 521
  • 4
  • 9
1

I used GOTO when I write script for working under cli mode. It save my life.

EThaiZone
  • 311
  • 1
  • 8
  • I use goto in this micro framework that I wrote. [CLIP Framework](https://github.com/ethaizone/CLIP) 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. – EThaiZone Sep 17 '13 at 02:20
  • 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. – Mike Q Jul 28 '16 at 14:08
  • @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 Sep 26 '16 at 09:38
  • 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. – EThaiZone Sep 26 '16 at 09:42