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).