Goto in PHP
Moderator: General Moderators
Re: Goto in PHP
I did not realize that... i feel kinda stupid now
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Goto in PHP
Well then you would also have to argue that switch() has the same problem -- it was structured programming's last attempt to hiding everything but the last 1% of goto's use. All of the structured constructs are gotos -- but with scope as you rightly point out.pytrin wrote:In my opinion, a GOTO in the traditional sense is reuse without scope. This is dangerous, like using a global, but can be used to solve difficult problems very easily (but not elegantly - in my opinion). I liked though the loop example they had in that discussion volomike linked to, that could be a nice addition.
The one well known exception to the "goto bad" rule is parsers (and generally state machines). If you have done much work on parsers you probably know that there circumstances where not using goto produces much worse code that using it...
(#10850)
Re: Goto in PHP
I imagine that on a lower level GOTO are a must, since somewhere along the line you have to declare what functions are. But PHP is a much higher level language, where sufficiently advanced scoped structures are available, so I can't really see a good use for a GOTO. If someone has a good example, I would love to see it.
Re: Goto in PHP
I've never ever found a need for goto when writing PHP. I doubt that's going to change.
- volomike
- Forum Regular
- Posts: 633
- Joined: Wed Jan 16, 2008 9:04 am
- Location: Myrtle Beach, South Carolina, USA
Re: Goto in PHP
Is it okay to use switch() instead of if/else if/else control structures? I use switch() all the time and I like how I can layer things like so:arborint wrote:you would also have to argue that switch() has the same problem
Code: Select all
switch(Controller::Action) {
case '/register-candidate':
case '/register-employer':
Controller::Dispatch('Registrations/Register');
break;
case '/':
Controller::Dispatch('Home/Login');
break;
default:
Controller::Dispatch(404);
break;
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Goto in PHP
The one example I gave is parsers. That is usually given as an a good case for inclusion for goto.pytrin wrote:If someone has a good example, I would love to see it.
But your past experience is not the basis for designing language features.Syntac wrote:I've never ever found a need for goto when writing PHP. I doubt that's going to change.
I use switch occasionally as well. I was just pointing out that it is really just a structured goto -- and a pretty blatant one at that. That is typically why programmers tend to intuitively shy away from using switch().volomike wrote:Is it okay to use switch() instead of if/else if/else control structures? I use switch() all the time and I like how I can layer things like so:
And to be clear, the language feature being added is not goto but break to label. I think it is a nice compromise addition to the language because, as I said about, using this kind of goto in looping state machines, such as parsers, can turn a horrible mess of structured constructs into much cleaner, more understandable code. It may be a rare use case but it is a real one.
(#10850)
Re: Goto in PHP
I asked for an example in PHP. As far as I know, PHP is not meant to build language parsers.The one example I gave is parsers. That is usually given as an a good case for inclusion for goto.
The main difference between a switch or if/else and a GOTO, is that those structures can't go back in the code and violate the expected direction of statement evaluation flow - from the first row to the last row. To me that is like going back in time (ie, forbidden. did you see back to the future?I was just pointing out that it is really just a structured goto
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Goto in PHP
How could one be given -- there is no goto in PHP! The example I gave is one for why goto is included in general in languages.pytrin wrote:I asked for an example in PHP.
Again, you may not want to write them. There are certainly parsers for YAML, JSON, etc. done in PHP. The question of whether you or I would write a parser in PHP, or use goto, is separate issue from whether anyone might want to write a parser or whether goto should be included in the language.pytrin wrote:As far as I know, PHP is not meant to build language parsers.
(#10850)
Re: Goto in PHP
I meant a good example where a GOTO might simplify something in PHP...How could one be given -- there is no goto in PHP! The example I gave is one for why goto is included in general in languages.
I'm not talking about me wanting or not to do something, I'm talking about appropriateness of the feature to the language. PHP was not meant to be a general purpose language, though it can do many things. Asking for a potentially troublesome to implement features the language is not intended for is not a convincing case.Again, you may not want to write them
And I thought you meant programming languages parsers, not data structures like JSON or YAML. Maybe you could give me a good example with one of those?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Goto in PHP
As I said -- parsers. Specifically looping state machines. The reason goto is useful in these is that you often end up with complex if() and switch() statements to deal with skipping over sections of code in the loop given the current state. The use of gotos cleans up this code -- in the opinion of some, though others may say it does not. If you have ever looked at the output of yacc or lex you would know what I mean. As I have said, it is not a very common case. But as I have said, it is one real case that obviously a number of PHP programmers have voiced an interest in. It is quite possible that even with these examples you may still disagree.
And I seriously doubt that many PHP programmers are going to hurt themselves on break to label...
And I seriously doubt that many PHP programmers are going to hurt themselves on break to label...
(#10850)
Re: Goto in PHP
I actually said that the example they gave (break to label) is a nice one 
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Goto in PHP
Yikes! You mean after all of that we agree that goto in general is a bad idea but in certain narrow cases that something like break to label might be handy!!!

(#10850)