14

I know, there are other questions about the goto statement introduced in PHP 5.3.

But I couldn't find any decent answer in there, all were of the type "last resort", "xkcd", "evil", "bad", "EVIL!!!". But no valid example. Only statements that there aren't any uses. Or statements that there are some rare use cases (again, without examples).

So, the question is: "What are the valid use cases of goto in PHP?". Answers for "Is goto evil?" are not welcome and will get downvoted. Thanks :)

Or does somebody have a link to an RFC where the decision is explained - I couldn't find one.

Community
  • 1
  • 1
NikiC
  • 100,734
  • 37
  • 191
  • 225
  • `goto` is convenient as red herring in discussions about the useful- or evilness of language constructs, regardless of any practical use. – mario Jan 30 '11 at 16:18
  • 1
    @mario: I tried to make clear in my question, that I'm not interested in knowing whether or not it is evil. Such questions are a waste of time. I'm interested in concrete examples. And thus I consider this not to be a "subjective or argumentative" question. – NikiC Jan 30 '11 at 16:29
  • @NikiC, Btw when you say that you're a ["PHP core developer"](http://stackoverflow.com/users/385378/nikic), does that mean that you know C? – Pacerier Jul 12 '15 at 16:55
  • Some kind of bot (eg. Telegram's) or something based on a finite state machine (eg. dining kiosks, digital menus, etc.) – Life after Guest Jan 05 '21 at 14:29

5 Answers5

6

One possible use (that can however be be implemented using other means) is implementation of a finite state machine.

NikiC
  • 100,734
  • 37
  • 191
  • 225
Mchl
  • 61,444
  • 9
  • 118
  • 120
  • 1
    Is a finite state machine something like a parser? – NikiC Jan 30 '11 at 15:08
  • Hm, that doesn't help me much. Could you give an example of a finite state machine or where it is used? The Wiki article is pretty theoretical, I couldn't find an application in there. – NikiC Jan 30 '11 at 15:24
  • 1
    Part of it comes from the fact, that FSM don't really find usage in the area where PHP is typically used (web apps). FSM is a construct that can be in one of predifined states (like elevator on given floor) and if a specific condition is met (like someone pressing a button) then FSM performs a set of actions called a transition (start the engine) in order to reach a new desired state (move to another floor). – Mchl Jan 30 '11 at 15:31
  • Ah, okay. So my lexer/parser thought was in the right direction. – NikiC Jan 30 '11 at 15:38
  • Yes, it seems FSMs are being used for that. – Mchl Jan 30 '11 at 15:39
5

See PHP Architect - GOTO in PHP 5.3: is it Really That Evil?

This article gives a short overview of why GOTO can be useful, why we have it in PHP (since 2004) and why it was/is controversial. The general answer seems to be: GOTO is mostly useless and should be avoided unless in very narrow application spaces (like building a compiler):

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.

Also see some of the original newslist discussion/controversy mentioned in the article:

Also see this thorough language agnostic discussion about GOTO considered harmful at C2 Wiki

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 1
    Okay, so again finite state automaton, lexing, parsing, compiling. That seems to be the number one (or even the only) use case. – NikiC Jan 30 '11 at 15:41
4

One case where I use it is when I have multi-level checking to do and don't want to add extra code when I could just jump to the relevant section after my checks.

For example, there's this system that checks the status of various machines in a warehouse, and fails the warehouse as a whole and sends out an alert if any one part of any one system is dead, in pseudo-code it goes like this:

if(hsensor[status] != OK) {
    alert = Humidity sensor hsensor[fail_id] failed with error hsensor[fail_error]
    goto sendAlert;
}

if(cosensor[status] != OK) {
    alert = Co2 sensor cosensor[fail_id] failed with error cosensor[fail_error]
    goto sendAlert;
}

That way, if anything fails, I can skip all other checks and go straight through to the alert. Whoever is on call receives the alert and pulls up the full result of whatever function called the alert so that they can fix it.

hdezela
  • 536
  • 6
  • 16
  • 2
    This is the best use case that I know of. The alternative is a bunch of nested if statements or a global status variable that you check in every if statement. In my opinion, goto is the cleanest way to accomplish this. – Brian Adams May 15 '15 at 16:17
2

goto operator can be used to break multi-level loops.

This particular goto application example is given at goto manual page:

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.

Kel
  • 7,680
  • 3
  • 29
  • 39
  • 3
    Don't we have `break x;` for that? `break 4;` will break four loops. [Manual for `break`](http://php.net/break) – NikiC Jan 30 '11 at 15:13
  • Yes, we have. BTW, if used properly, `goto` can lead to little clearer code than `break x`. Maybe this is an attempt to provide breaking mechanism similar to labeled break / continue in Java. – Kel Jan 30 '11 at 15:21
  • 3
    I would appreciate an example where a `goto` is cleaner when a `break x`. I can't imagine one myself. – NikiC Jan 30 '11 at 15:27
  • Personally I can't remember any real-life PHP example of such complex nested loop, where `break x` would be not enough. All I want to say is: generally label can contain little more information than just a number in `break x`, and theoretically it may be useful. – Kel Jan 30 '11 at 15:52
  • @Kel, **If this was the reason**, then PHP would have added labels to break, they wouldn't have added `goto`. – Pacerier Jul 12 '15 at 16:52
2

Goto has been in large replaced by specialized statements like continue, break, try...catch. In the years I'm working with PHP, I didn't feel it could help me once. In languages like C++ which don't have break x statement it can be used to jump out of nested loops, but since PHP has it, there is no reason.

Code Complete has very nice and detailed chapter about gotos. Quoting the final two paragraphs:

Use of gotos is a matter of religion. My dogma is that in modern languages, you can easily replace nine out of ten gotos with equivalent sequential constructs. In these simple cases, you should replace gotos out of habit. In the hard cases, you can still exorcise the goto in nine out of ten cases: You can break the code into smaller routines, use try-finally, use nested ifs, test and retest a status variable, or restructure a conditional. Eliminating the goto is harder in these cases, but it's good mental exercise and the techniques discussed in this section give you the tools to do it.

In the remaining one case out of 100 in which a goto is a legitimate solution to the problem, document it clearly and use it. If you have your rain boots on, it's not worth walking around the block to avoid a mud puddle. But keep your mind open to goto-less approaches suggested by other programmers. They might see something you don't.

I find it very hard to understand why the PHP team bothered to add it at all (after all those years we lived in peace without it).

Community
  • 1
  • 1
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
  • 4
    In PHP however `goto` was added when all the statements you mention have already been long available. Hence the question I presume. – Mchl Jan 30 '11 at 15:23
  • @Mchl, Exactly. The question is why `goto` was added. – Pacerier Jul 12 '15 at 16:49