php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #29287 Request: Line labels and goto
Submitted: 2004-07-20 21:58 UTC Modified: 2006-10-27 01:39 UTC
Votes:11
Avg. Score:4.6 ± 1.1
Reproduced:9 of 10 (90.0%)
Same Version:4 (44.4%)
Same OS:3 (33.3%)
From: abodeman at yahoo dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 6 OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
28 - 13 = ?
Subscribe to this entry?

 
 [2004-07-20 21:58 UTC] abodeman at yahoo dot com
Description:
------------
As I have outlined in the PHP manual in user comments under "break", the "break n" syntax is obnoxious. Code containing a "break n" can silently become broken if it is surrounded by another loop; I believe it is the only language feature that will break this way. It is also difficult to figure out where control resumes after a "break n"; you have to count loops and switch statements, but ignore if statements.

With this in mind, I propose a much better syntax, one that has been time-tested and proven to work well in other languages: line labels. By adding line labels to PHP, you can say "break label;" and break out of the loop or switch statement labeled as "label". Putting more loops in the code won't break anything, and it is easier to determine where control will resume.

While you're at it, a "goto" statement would also be very much appreciated in PHP. Yes, I know, the vast majority of programmers are yelling at me now, because they have been inundated by books and teachers telling them that goto is always 100% evil. The truth of the matter is that goto is sometimes the natural way to do things. Sure, it's possible to write code without goto, but it's also possible to write code without switch statements or for loops. And eliminating goto from a language does not automatically make well-written code.

Consider the following scenario: There is an if statement inside a for loop inside a switch statement inside two while loops. The situation is not uncommon. Now, inside this if statement are some 60 lines of code. The fifth line of code is some test; if the test fails, execution should jump just past the end of the if statement, inside the for loop. Currently the only way to do that is to wrap the remaining 55 lines of code inside yet *another* if statement, adding yet *another* level of indenting. Ick.

Obviously the natural solution here is to have a goto statement that jumps to where it needs to go. But PHP doesn't have anything that will work. There's not even a good kludge for it. If you didn't want to wrap the 55 lines in an if statement, you could wrap the 60 lines in a do { ... } while (0); and just use a break, but that's even worse.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-20 22:31 UTC] derick@php.net
We won't implement goto and or labels.
 [2004-07-21 00:00 UTC] jhelzer at cse dot unl dot edu
I am curious, why not?
 [2004-07-26 19:44 UTC] jhelzer at cse dot unl dot edu
Whether this is the relevant place or not, to treat goto and labels as two heads of the same body (as you seem to imply) is silly.  I can understand a reluctance to implement goto, but adding labels for the break statement make a tremendous amount of sense.
 [2004-08-02 11:42 UTC] derick@php.net
It's still likely to be added ;-)
 [2004-08-02 21:56 UTC] jdemaris at cse dot unl dot edu
What is still likely to be added?  Labels?  Gotos?  Both?

Many developers, myself included, would welcome both features.  I'm curious why you were so quick to spout an unqualified "no" to the original post.
 [2004-08-02 22:32 UTC] pollita@php.net
The original post got a "no" because it's long been an unspoken position that GOTO has no place in PHP.

As chance would have it, I submitted a goto implementation to the php.internals list last on the 28th (hadn't seen your feature request at the time interrestingly enough) which spawned somewhere over 150 posts on the mailing list plus at least a couple blog entries on the subject.

The implementation is still under development, and the decision to implmenent it is.... under discussion.  Feel free to chime in on internals@lists.php.net  You can read up on the conversation in the archives at http://news.php.net/php.internals/ 

 [2006-10-27 01:39 UTC] magnus@php.net
(Limited) goto was added some time ago to HEAD (PHP 6).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC