

Bug #48669: PHP now includes GOTO - roder
http://bugs.php.net/bug.php?id=48669

======
jdale27
"Gotos aren't damnable to begin with. If you aren't smart enough to
distinguish what's bad about some gotos from all gotos, goto hell."

\-- Erik Naggum,
[http://groups.google.com/group/comp.lang.lisp/msg/bb32931876...](http://groups.google.com/group/comp.lang.lisp/msg/bb32931876c3a7cd)

~~~
TimMontague
A few years back, Linus and some kernel hackers had a good discussion about
the good gotos vs. the bad gotos.

<http://kerneltrap.org/node/553>

------
snorkel
"why turn the language into a public menace?"

Too late.

------
voidpointer
If there was any doubt left that PHP is the VisualBasic of the web it should
be cleared now.

BTW, I think nobody linked the classic goto critique from Dijkstra yet:
[http://www.cs.utexas.edu/~EWD/transcriptions/EWD02xx/EWD215....](http://www.cs.utexas.edu/~EWD/transcriptions/EWD02xx/EWD215.html)
(or paywalled in ACM context at
<http://portal.acm.org/citation.cfm?id=362929.362947> )

Dijkstra was pretty specific about the cases where goto would be confusing and
made a case that one should would be better off just not using it at all. His
argument applies much more to gotos that can jump across procedure boudaries.
Most surviving gotos (including the PHP goto) are not that unconstrained so
they do represent a compromise between Dijkstra's position and the pragmatic
use of goto where it actually improves the readability of code - especially
when exceptions are not available for some reason.

------
ivanstojic
Next up, PHP 6 will include ComeFrom.

~~~
kwantam
comefrom is an excellent and seriously useful instruction.

My team has (kind of) implemented it in hardware as a way of patching ROM code
in RAM. When the PC becomes equal to an address in the comefrom table, the PC
is loaded with a RAM address and execution continues there.

If you have a big chunk of code that could need small tweaks, storing it in
ROM and implementing a small RAM alongside it saves a lot of chip area.

I'm almost 100% sure Freescale does this on some of their 32-bit
microcontrollers.

Yes, I design ICs. No, I do not work for Freescale.

------
commiebob
<http://xkcd.com/292/>

~~~
mixmax
I must be psychic, I just knew that link would show up in the comments.

~~~
Scriptor
But the top one?

~~~
mcav
Never underestimate the power of XKCD.

------
jacquesm
Finally, it is now possible to produce true spaghetti code in PHP.

I wonder why this took so long. To this date the oversight of omitting goto
from a language that otherwise seemed so geared towards obfuscation puzzled me
greatly.

~~~
shaunxcode
On the flipside it certainly makes implementing TCO a lot more possible for
those of us generating php.

~~~
nir
So, to summarize, it's useful for people building certain projects with PHP
and at the same time a great boon for people making sarcastic comments about
PHP. Win win.

------
run4yourlives
I'll admit, I loled.

What is the reasoning behind goto in PHP though? I know php well enough, and I
can't think of a time or place I've ever said: "I really wish I had a goto
here!"

~~~
mrshoe
Commonly cited use cases include:

    
    
        1. Break out of nested loops.
        2. Jump to cleanup code at the end of a function instead of an early return.
    

I never use goto, but I think the usual knee jerk reaction to it is sometimes
unwarranted.

~~~
tilly
If you have them in your language the common better solutions to those use
cases include:

1\. Labeled loop control 2\. Exceptions

PHP has #2 and a bad form of #1. In PHP you get to say how many loops to break
out of, but not the name of the loop to go to. This makes code less self-
documenting and can be fragile when editing code.

Other uses of goto for which substitutes are harder to find are to to make
your language a more convenient compilation target, and when interacting with
other control structures not under your control. These uses are VERY rare. In
a decade of professional Perl programming I have seen each used twice. None of
the examples were in any code base I worked with. Two were in the Perl core,
the first is that the s2p utility emits gotos to emulate branches in sed, and
the second is that the original Switch.pm used a goto to break out of any
loops that it happened to interact with. (That one was a computed goto! After
all he didn't know how this switch might interact with other switches.)

I would be willing to use goto for nested loops in PHP, but only because PHP
does not offer labeled loop control. While I like to believe that I understand
when to use goto for other reasons, I doubt I'll ever encounter a serious need
to do that in any language.

~~~
jerf
Third example: Writing a Python-style generator in a language that doesn't
have it. There's a trivial rewrite using gotos that means you mostly get the
same logic, but you need goto.

I think I've used that twice in 10 years, but when you need it, you need it.
At the cost of uglying up one function you can clean up tens or hundreds of
invocation points, each of which involves duplicate (and hard-to-factor-out
without-this-trick) logic. (This _is_ the trick to factoring it out.) I'll pay
that gladly.

------
extension
Control structures are high level abstractions around common patterns found
with jumps and conditional branches. Many such patterns have been identified,
ranging in complexity from simple loops and conditionals to non-local returns
to cooperative multi-threading.

To assert that there is no possible good use for GOTO implies that every
conceivable control flow pattern has a more clean and intuitive implementation
using only the high level control structures.

When you consider the variation, from language to language, in the selection
of control structures available, the conjecture seems absurd. At the very
least, GOTO is a reasonable compromise when forced to use a language like C,
which lacks structures for such common patterns as exceptions, among others.

What is certainly worse than using GOTO is contorting code to use high level
control structures in counter-intuitive ways, or worse yet, implementing ad-
hoc control mechanisms using conditional variables. For purposes of
readability, an informatively labeled GOTO is greatly preferable to such
obfuscation.

Fortunately, we have versatile languages like LISP and Ruby which provide a
rich library of control flow abstractions and allow us to create our own using
closures. For the rest of the world, let them have their GOTO.

------
uninverted
I bet a lot of Basic code can be compiled to PHP just by changing the caps.

~~~
eru
And colons to semicolons. (Does Basic still use them to separate statements on
the same line?)

------
Legion
It's kind of chuckle-worthy because it's being added in so late in the game,
and because of the smart-ass bug report, but this is a non-issue.

In saying this, I take my life into my own hands, as you can now bet that I
will be forced in the future to work on fixing some code written by someone
that takes this new feature and _runs_ with it.

------
humbledrone
I was under the impression that the addition of "goto" was a joke. Are people
actually using it? While there are legitimate uses for "goto," they are pretty
much isolated to low-level C code, and are made unnecessary in a language that
supports exceptions...

~~~
huhtenberg
Exceptions vs. goto-based cleanup is purely a matter of personal programming
taste and habits. Gotos exist because there are people who find them
convenient, and not because gotos are "legitimate" in the context of that
particular programming language.

Making the language minimalistic is never a goal of a language design unless
it's a Brainfuck. If it _were_ a goal, there would've not been three loop
constructs in any modern languages.

~~~
eru
Scheme does not have _any_ loop constructs. Neither does Haskell. (Apart from
the standard library. There you find some `simulators' of loops.)

------
vegashacker
Don't miss the

    
    
      goto hell;
    

at the bottom. Awesome.

------
bartl
The label, is that global? The docs say:

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

but that doesn't quite imply that the label names don't need to be unique.

That would be quite typical for PHP, IMHO, with its global scope of top level
variables (= not in a function) of old.

------
mildweed
Previous thread: <http://news.ycombinator.com/item?id=649507>

------
vlucas
I am an avid PHP developer, and I can't believe this actually did get added
into PHP5.3. Even when I was a total noob I never wished the language had a
'goto' feature. I don't understand why this needs to be in a scripting
language.

I guess now it will be even easier to spot bad PHP code. That's the only plus
I can think of.

~~~
hvs
It's usually pretty easy to spot bad PHP code anyway. Just open it in a text
editor.

------
ashleyw
Wait, wait, wait...GOTO in PHP is _real_?? I thought it was a joke when I
heard about it a while back...

Those who use PHP properly don't need this, and the ones who currently write
spaghetti....well, will now write even worse spaghetti! At least the good
developers now have another way to spot bad code, I guess..

------
KevBurnsJr
That's pretty funny. But 5.3 did also come with lambdas, anonymous functions,
closures and namespaces. [http://www.recessframework.org/page/map-reduce-
anonymous-fun...](http://www.recessframework.org/page/map-reduce-anonymous-
functions-lambdas-php)

------
dunk010
I can understand the use as a labeled jump for loops, that's handy. Are there
other cases where this is useful? I'm thinking along the lines of things like
Quercus.

~~~
dmoney
When you're writing something that's modeled as a state machine, it's easier
to use goto to jump between the states, rather than simulating it with a while
loop, state variable, and case statement. Making the program's behavior
conform to a flow chart would probably be about the same.

I wonder: would goto have a better reputation if flowcharts were used more
often?

------
RyanMcGreal
_[4 Aug 6:47pm UTC] andrei@php.net_

Win.

------
edw519

      Goto floor(10);
      window.open();
      jump();

~~~
caillou
you are aware that PHP uses -> rather than . to access an object method? ;)

