# GOTO operator

- Thread: https://externals.io/message/11599
- Original archive: https://news-web.php.net/php.internals/11599
- Started by Sara Golemon on 2004-07-29T02:12:40+00:00
- Messages: 66
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/11599.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #11599 — Sara Golemon — 2004-07-29T02:12:40+00:00

URL: https://externals.io/message/11599#11599

I wrote up a patch for implementing gotos in php scripts a couple months ago
as an exercise in working with the Zend engine.  I put it aside assuming
noone would actually want it, but Wez and Ilia convinced me to post it for
consideration:

Description:
http://pecl.org

Patch:
http://pecl.org/patches/opcode_goto_5.1.0.diff

Talk amongst yourselves...

-Sara

---

## #11600 — Robert Cummings — 2004-07-29T02:21:48+00:00

URL: https://externals.io/message/11599#11600 · In reply to: #11599

> I wrote up a patch for implementing gotos in php scripts a couple months ago
> as an exercise in working with the Zend engine.  I put it aside assuming
> noone would actually want it, but Wez and Ilia convinced me to post it for
> consideration:
> 
> Description:
> http://pecl.org
> 
> Patch:
> http://pecl.org/patches/opcode_goto_5.1.0.diff
> 
> Talk amongst yourselves...

Oh that's awesome... please say yes... please please please :) I've
always thought goto was a bit of a missing element in PHP since it's the
method of choice IMHO for fast and concise parsing.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

---

## #11601 — Edin Kadribasic — 2004-07-29T02:41:18+00:00

URL: https://externals.io/message/11599#11601 · In reply to: #11599

> I wrote up a patch for implementing gotos in php scripts a couple months
> ago as an exercise in working with the Zend engine.  I put it aside
> assuming noone would actually want it, but Wez and Ilia convinced me to
> post it for consideration:

Big +1 from here. It will signficantly simplify my error handling code :)

If engine guru's don't have technical objections for this patch I think it 
should be applied right away!

Edin

---

## #11602 — Wez Furlong — 2004-07-29T02:47:34+00:00

URL: https://externals.io/message/11599#11602 · In reply to: #11599

The patch looks ok to me.
One issue to be worked out is to prevent goto from jumping out of a
try..catch block.

goto is that nice tool that everyone is brainwashed to believe is
evil, instead of simply being taught how to use it properly.

+10 ;)

--Wez.


> I wrote up a patch for implementing gotos in php scripts a couple months ago
> as an exercise in working with the Zend engine.  I put it aside assuming
> noone would actually want it, but Wez and Ilia convinced me to post it for
> consideration:
> 
> Description:
> http://pecl.org
> 
> Patch:
> http://pecl.org/patches/opcode_goto_5.1.0.diff
> 
> Talk amongst yourselves...
> 
> -Sara

---

## #11603 — Ilia Alshanetsky — 2004-07-29T02:55:53+00:00

URL: https://externals.io/message/11599#11603 · In reply to: #11599

Goto is an extremely useful for parsers and makes life much easier when 
dealing with complex error handling situations.

+1

Ilia


> I wrote up a patch for implementing gotos in php scripts a couple months
> ago as an exercise in working with the Zend engine.  I put it aside
> assuming noone would actually want it, but Wez and Ilia convinced me to
> post it for consideration:
>
> Description:
> http://pecl.org
>
> Patch:
> http://pecl.org/patches/opcode_goto_5.1.0.diff
>
> Talk amongst yourselves...
>
> -Sara

---

## #11604 — Sara Golemon — 2004-07-29T02:59:26+00:00

URL: https://externals.io/message/11599#11604 · In reply to: #11602

> The patch looks ok to me.
> One issue to be worked out is to prevent goto from jumping out of a
> try..catch block.
>
After you mentioned that in IRC I tried out several test cases each of which
behaved well, but I of course could have missed some possible combination...

Btw, goto labels are considered op_array local, so you can't jump out of one
function into another function, or in/out of the main scope.

-Sara

---

## #11608 — Stefan Esser — 2004-07-29T10:05:59+00:00

URL: https://externals.io/message/11599#11608 · In reply to: #11599

Hi,

> I wrote up a patch for implementing gotos in php scripts a couple months ago
> as an exercise in working with the Zend engine.  I put it aside assuming
> noone would actually want it, but Wez and Ilia convinced me to post it for
> consideration:

I was really rolling on the floor when I saw this mail, because I faked 
up such a patch as a joke a few days ago.

I love gotos. Try/Catch is goto for poor people ;)

+2^17,3 from me


Stefan

---

## #11610 — Paul G — 2004-07-29T10:19:48+00:00

URL: https://externals.io/message/11599#11610 · In reply to: #11608

----- Original Message ----- 
From: "Stefan Esser" <sesser@php.net>
To: <"Sara Golemon"@utopia.rusko.us>; <internals@lists.php.net>
Sent: Thursday, July 29, 2004 6:05 AM
Subject: Re: [PHP-DEV] GOTO operator


> Hi,
>
> > […]
ago
> > […]
for
> > […]
>
> I was really rolling on the floor when I saw this mail, because I faked
> up such a patch as a joke a few days ago.
>
> I love gotos. Try/Catch is goto for poor people ;)
>
> +2^17,3 from me

tongue out of cheek + my opinion doesn't count,

goto is *very* useful for error handling when you've got stuff to clean up
that's common between error conditions. absence of goto is a major
annoyance. i'd like to see it in 4.x as well, but i realize that ain't gonna
happen officially.

paul

---

## #11611 — Georg Richter — 2004-07-29T13:26:07+00:00

URL: https://externals.io/message/11599#11611 · In reply to: #11603

Am Do, den 29.07.2004 schrieb Ilia Alshanetsky um 4:55:
> Goto is an extremely useful for parsers and makes life much easier when 
> dealing with complex error handling situations.

+1

/Georg

---

## #11612 — Alex Pagnoni — 2004-07-29T13:31:32+00:00

URL: https://externals.io/message/11599#11612 · In reply to: #11603

Alle 04:55, giovedì 29 luglio 2004, Ilia Alshanetsky ha scritto:

> Goto is an extremely useful for parsers and makes life much easier when
> dealing with complex error handling situations.

Does a goto operator make sense in a language that wants to be more and more 
OOP oriented and that has just introduced a new OOP model? Not so much for 
me.

-1

---

## #11613 — Leonardo Pedretti — 2004-07-29T14:17:09+00:00

URL: https://externals.io/message/11599#11613 · In reply to: #11599

Goto is somewhat equivalent to 'case' combined with using 'continue' in loops. 
More precisely, this 2 language constructions are the 'goto' implementation 
with a different name so that Dijkstra does not get back from the grave to 
punish us =D
It is true that goto is a little 'off-paradigm' in OOP and truly structured 
languages, however, the point is this: sometimes continue and case produce 
code that is not so clear and small at the same time as a little well used 
goto. BUT goto allows non experienced or not well educated programmers to 
begin writing horrible and totally unstructured code, and go further 
developing bad programming habits... IMHO, would php be a more restrictive 
language (strongly typed, or something alike) I would be totally and 
unconditionally on for a goto addon. However, i see the bad habits and bad 
code promotion as something that should be very carefully noted before 
deciding whether to apply it or not.
I hope i have added some good points to help make the consideration deeper and 
better =)

Regards
Leo



> I wrote up a patch for implementing gotos in php scripts a couple months
> ago as an exercise in working with the Zend engine.  I put it aside
> assuming noone would actually want it, but Wez and Ilia convinced me to
> post it for consideration:
>
> Description:
> http://pecl.org
>
> Patch:
> http://pecl.org/patches/opcode_goto_5.1.0.diff
>
> Talk amongst yourselves...
>
> -Sara

-- 

Leonardo Pedretti
Axon Sistemas
Líder de Equipo Proyecto Basalto

---

## #11616 — Lukas Smith — 2004-07-29T14:40:05+00:00

URL: https://externals.io/message/11599#11616 · In reply to: #11613

Leonardo Pedretti wrote:

> Goto is somewhat equivalent to 'case' combined with using 'continue' in loops. 
> More precisely, this 2 language constructions are the 'goto' implementation 
> with a different name so that Dijkstra does not get back from the grave to 
> punish us =D

also with exceptions there are also ways to "emulate" goto ...

might as well give people the real thing .. that is much more in the 
spirit of php, where the aim is to make it very easily readable what you 
are actually up to in your code.

regards,
Lukas

---

## #11617 — Robert Cummings — 2004-07-29T14:40:20+00:00

URL: https://externals.io/message/11599#11617 · In reply to: #11613

> Goto is somewhat equivalent to 'case' combined with using 'continue' in loops. 
> More precisely, this 2 language constructions are the 'goto' implementation 
> with a different name so that Dijkstra does not get back from the grave to 
> punish us =D
> It is true that goto is a little 'off-paradigm' in OOP and truly structured 
> languages, however, the point is this: sometimes continue and case produce 
> code that is not so clear and small at the same time as a little well used 
> goto. BUT goto allows non experienced or not well educated programmers to 
> begin writing horrible and totally unstructured code, and go further 
> developing bad programming habits... IMHO, would php be a more restrictive 
> language (strongly typed, or something alike) I would be totally and 
> unconditionally on for a goto addon. However, i see the bad habits and bad 
> code promotion as something that should be very carefully noted before 
> deciding whether to apply it or not.
> I hope i have added some good points to help make the consideration deeper and 
> better =)

While it is true you can approximate the functionality of goto using
switch, or if/elseif/else constructs, these are inferior in efficiency.
Evaluation via switch or if/elseif/else system is O(n) just to get to
the desired block of code. Using goto, getting to the chosen code block
is O(1) (see note below). This is probably the most important reason why
many parsing implementations use goto.

Note
----
Goto jumping is O(1) if getting to the jump destination doesn't require
a hash lookup due to "wuring" of the destination at compile time. If a
hash lookup is required then it is probably something like O(lg n) which
is still superior in efficiency to O(n). Additionally, while it is true
that an O(lg n) lookup might be necessary at compile time to find the
pointer to the labeled code block, this is a one time event and has
little bearing on the efficiency overall when during runtime the goto
might be hit thousands or millions of times.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

---

## #11618 — Sara Golemon — 2004-07-29T15:04:53+00:00

URL: https://externals.io/message/11599#11618 · In reply to: #11617

> Note
> ----
> Goto jumping is O(1) if getting to the jump destination doesn't require
> a hash lookup due to "wuring" of the destination at compile time. If a
> hash lookup is required then it is probably something like O(lg n) which
> is still superior in efficiency to O(n). Additionally, while it is true
> that an O(lg n) lookup might be necessary at compile time to find the
> pointer to the labeled code block, this is a one time event and has
> little bearing on the efficiency overall when during runtime the goto
> might be hit thousands or millions of times.
>
*this* implementation of GOTO requires a hash lookup, however it could be
reworked to use the same backpatching as the ZEND_JMP ops used with
conditional statements.  While doing that would speed execution time by
skipping the hash lookup, it'd slow compile time and remove the ability to
use complex expressions as goto targets (i.e.  goto "foo$i"; ).

-Sara

---

## #11619 — Robert Cummings — 2004-07-29T15:36:43+00:00

URL: https://externals.io/message/11599#11619 · In reply to: #11618

> > […]
> *this* implementation of GOTO requires a hash lookup, however it could be
> reworked to use the same backpatching as the ZEND_JMP ops used with
> conditional statements.  While doing that would speed execution time by
> skipping the hash lookup, it'd slow compile time and remove the ability to
> use complex expressions as goto targets (i.e.  goto "foo$i"; ).

I'm not sure whether this is feasible or not, but perhaps a mix of
compile-time/runtime logic is possible. If the argument of the goto is a
literal then a compile-time jump hook is created, otherwise the argument
is evaluated at run-time. This would provide the best of both worlds,
but as I said I have no idea as to its feasibility.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

---

## #11621 — Timm Friebe — 2004-07-29T21:25:56+00:00

URL: https://externals.io/message/11599#11621 · In reply to: #11599

> I wrote up a patch for implementing gotos in php scripts a couple months ago
> as an exercise in working with the Zend engine.
[...]

In the five or six year's I've been programming PHP, I haven't ever felt
the need to have "goto". Neither have I seen a newsgroup posting related
to it in the two years or so I followed the German PHP newsgroup,
de.comp.lang.php.

I don't think goto is really needed. People using other languages manage
to do without it, too.

So, -1.

- Timm

---

## #11623 — Robert Cummings — 2004-07-29T22:11:16+00:00

URL: https://externals.io/message/11599#11623 · In reply to: #11621

> 
> > […]
> [...]
> 
> In the five or six year's I've been programming PHP, I haven't ever felt
> the need to have "goto". Neither have I seen a newsgroup posting related
> to it in the two years or so I followed the German PHP newsgroup,
> de.comp.lang.php.
> 
> I don't think goto is really needed. People using other languages manage
> to do without it, too.

C has it
C++ has it
VB has it
Lisp has "GO" which is pretty much the same
Perl

I'm sure many other too.

Just because people "manage" to do without it in some languages, doesn't
mean it isn't a useful feature.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

---

## #11624 — Ilia Alshanetsky — 2004-07-29T22:18:13+00:00

URL: https://externals.io/message/11599#11624 · In reply to: #11622

> Is the average Joe going to write parsers in the everydays work? Probably
> not. But give him the power of goto and (s)he is going to shoot him/her in
> the legs, in the heart, in the head. And even not only himself because
> (s)he will create a legacy code which someone else has to understand and
> fix. We have `extract()` and we say in the docs it is not good to use it to
> populate variables but people hack and workaround register_globals set to
> off by just using this function. IMO PHP has enough power even without
> "goto". Left aside what Dijkstra has written. goto code tends to become
> spaghetti one. Sometimes I miss it when write PHP scripts (for example
> jumping between 2 clauses in a switch, something which is used in
> `var_dump()` C implementation) but then I reconsider that it is better this
> way.

Parsers are just one place where GOTOs show their merit. GOTOs can be 
immensely useful error handling mechanism that can save having to make dozens 
of functions just to avoid code duplication. As I had mentioned earlier GOTO 
can be used to eliminate stack smashes caused by recursive functions that are 
the simplest solution (according to average Joe) to many problems.
IMHO just saying something can be abused does not seem like a good reason, 
because that can applied to anything. Exceptions can be abused, OO can be 
abused, xml can be abused, etc... 

Ilia

---

## #11626 — Ilia Alshanetsky — 2004-07-29T22:30:55+00:00

URL: https://externals.io/message/11599#11626 · In reply to: #11625

Andrey,

Here are some more programming languages you may wish to discredit for their 
goto support.

Fortran,
C#,
Cobol,
Ada

Ilia



> Robert Cummings wrote:
> > […]
>
> Well, yes, C is very close the ASM, and ASM needs jmp-s.
>
> > […]
>
> Yes, C++ was designed to be as close to C as possible. I think
> I read this in Straustrup's book. Thus C++ had to have goto.
>
> > […]
>
> Yes, since it has it roots in Basic. Remember
> 10 Print "Hello"
> 20 Goto 10
>
> > […]
>
> No experience with Lisp :)
>
> > […]
>
> A language created by a hacker for hackers. I think Perl is quite
> famous for the unmaintable code.
>
> > […]
>
> cheers,
> andrey

---

## #11627 — Ilia Alshanetsky — 2004-07-29T22:37:37+00:00

URL: https://externals.io/message/11599#11627 · In reply to: #11621

> In the five or six year's I've been programming PHP, I haven't ever felt
> the need to have "goto". Neither have I seen a newsgroup posting related
> to it in the two years or so I followed the German PHP newsgroup,
> de.comp.lang.php.

I must admit that an average guestbook script does not need gotos and probably 
would work best without them. That said PHP now a days is used for a whole 
lot of different and exciting things, which in many instances can benefit 
from "goto" availability.

Ilia

---

## #11630 — Sara Golemon — 2004-07-29T22:41:36+00:00

URL: https://externals.io/message/11599#11630 · In reply to: #11622

> Is the average Joe going to write parsers in the everydays work? Probably
not.
> But give him the power of goto and (s)he is going to shoot him/her in the
legs,
> in the heart, in the head. And even not only himself because (s)he will
create a
> legacy code which someone else has to understand and fix.
>
function foo() { foo(); }
for($i = 0; $i < 10; $i--);
`php -f {$_SERVER['PHP_SELF']}`;
while (!($fp = fopen('/does/not/exist', 'r')));

All oversimplistic examples obviously, but the concepts aren't unfamiliar to
those of us that actually volunteer in the trenches on usenet, irc, forums,
etc...

To quote Wez: "GOTO doesn't kill code, bad programmers do."

> We have `extract()`
> and we say in the docs it is not good to use it to populate variables but
people
> hack and workaround register_globals set to off by just using this
function.
>
Taking away their tools will not make them code more intelligently, it'll
send them off to another language where they CAN continue making the same
mistakes.  The way to breed better programmers is to point out their
mistakes and suggest how they can correct them.  But not every
implementation of a particular concept is a mistake, if it were we'd have
only one CMS, only one gallery script, only one Blog, and come to that....
only one language.

> IMO PHP has enough power even without "goto". Left aside what Dijkstra has
> written. goto code tends to become spaghetti one.
>
I could just as easily state that nested code with lots of OOP does the same
thing.  I'll say it agin:

"GOTO doesn't kill code, bad programmers do."

> Sometimes I miss it when
> write PHP scripts (for example jumping between 2 clauses in a switch,
something
> which is used in `var_dump()` C implementation) but then I reconsider that
it is better
> this way.
>
Often it is better.  And sometimes it's not.  Funny how generalizations
work.

> Looks like I am -1 if that counts :)
>
Damn skippy it does.  Everyone's oppinion counts.  Whether the dicision is
made to add it or leave it out, the voices of this community will have made
an impact on that decision.

-Sara

---

## #11629 — Robert Cummings — 2004-07-29T22:42:07+00:00

URL: https://externals.io/message/11599#11629 · In reply to: #11625

> Robert Cummings wrote:
> > […]
> Well, yes, C is very close the ASM, and ASM needs jmp-s.

And yet it isn't ASM.

> > […]
> Yes, C++ was designed to be as close to C as possible. I think
> I read this in Straustrup's book. Thus C++ had to have goto.

Probably because C had a lot of good features. Isn't PHP also in many
ways modeled after C?

> > […]
> Yes, since it has it roots in Basic. Remember
> 10 Print "Hello"
> 20 Goto 10

I think, and I doubt I'm alone, that there a world of difference between
the classical goto syntax where you "goto" the line number and what is
now considered good goto practice by declaring a label and jumping to
it. Not only that but goto in modern practice is limited to where it can
jump by it's current scope (not sure if scope is quite the right word,
but you know what I mean).

> > […]
> No experience with Lisp :)

I dislike LISP :)

> > […]
> A language created by a hacker for hackers. I think Perl is quite
> famous for the unmaintable code.

Unmaintainable because of the ability to write cryptic code. There's
nothing cryptic about:

    ...code

    goto cleanup:

    ...code

    cleanup:

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

---

## #11631 — Paul G — 2004-07-29T22:43:44+00:00

URL: https://externals.io/message/11599#11631 · In reply to: #11622

----- Original Message ----- 
From: "Andrey Hristov" <php@hristov.com>
To: <ilia@prohost.org>
Cc: <internals@lists.php.net>
Sent: Thursday, July 29, 2004 8:00 PM
Subject: Re: [PHP-DEV] GOTO operator


--- snip ---

> Is the average Joe going to write parsers in the everydays work? Probably
not.

how about:

function pseudocode()
{
    $a=allocate_resource_z();
    $b=allocate_resource_y();

    $res=do_stuff();
    if(!$res)
        goto err_out;

    $c=allocate_resource_x();
    $res=do_more_stuff();
    if(!$res)
        goto err_out;

    $d=allocate_resource_foo();
    $res=do_even_more_stuff();
    if(!$res)
        goto err_out;

    return true;

err_out:
    free_resouce($a);
    free_resouce($b);
    if(isset($c)) free_resouce($c);
    if(isset($d)) free_resouce($c);

    return false;
}

here the do_stuff()s would be trivial inline code that makes no sense to
break out, each being a few slocs. everything looks neat and it is perfectly
clear what is going on.

with no goto:

function pseudocode()
{
    $a=allocate_resource_z();
    $b=allocate_resource_y();

    $res=do_stuff();
    if(!$res)
    {
        free_resource($a);
        free_resource($b);
        return false;
    }

    $c=allocate_resource_x();
    $res=do_more_stuff();
    if(!$res)
    {
        free_resouce($a);
        free_resource($b);
        free_resource($c);
        return false;
    }

    $d=allocate_resource_foo();
    $res=do_even_more_stuff();
    if(!$res)
    {
        free_resouce($a);
        free_resource($b);
        free_resource($c);
        free_resource($d);
        return false;
    }

    return true;
}

.. now imagine this isn't pseudocode, but actual code. you're doing a bunch
of stuff and the repetition becomes extremely unmanagable, especially if you
are doing miantenance later. ie, say you allocate another resource at the
beginning - add a free_resource() in one place only with the goto example,
in N places without.

before you discount the resource allocation stuff (in C this would be
primarily malloc()s), there is quite enough stuff that you can 'allocate' in
real code that isn't going to get gc'ed.

and yes, i've been missing goto dearly when doing real development. right
now, i'm emulating it by implementing allocation functions such that they
keep a list (or hashtable rather) of resources belonging to each function
and i can release them all in one line of code, but i'm still replicating
that line of code everywhere and, quite frankly, it's a kludge.

i agree with ilia - dont' castrate the language in a misguided effort to
protect the clueless. abuse of goto is best prevented by education on its
proper uses (few very specific cases).

paul

---

## #11633 — Sara Golemon — 2004-07-29T22:52:37+00:00

URL: https://externals.io/message/11599#11633 · In reply to: #11632

> do {
>    .....code...
>    if (something) break;
>    ...code....
> } while (0);
> ...cleanup code...
>
Are you suggesting a hack is better than the real thing?

-Sara

---

## #11635 — Edin Kadribasic — 2004-07-29T22:55:50+00:00

URL: https://externals.io/message/11599#11635 · In reply to: #11631

[snip]

This is exactly the kind of code that makes me miss goto so much for error 
handling.

Edin

---

Next page: https://externals.io/message/11599.md?page=2
