From:             abodeman at yahoo dot com
Operating system: Irrelevant
PHP version:      Irrelevant
PHP Bug Type:     Feature/Change Request
Bug description:  Request: Line labels and goto

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.


-- 
Edit bug report at http://bugs.php.net/?id=29287&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=29287&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=29287&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=29287&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=29287&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=29287&r=needtrace
Need Reproduce Script:      http://bugs.php.net/fix.php?id=29287&r=needscript
Try newer version:          http://bugs.php.net/fix.php?id=29287&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=29287&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=29287&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=29287&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=29287&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=29287&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=29287&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=29287&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=29287&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=29287&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=29287&r=float

Reply via email to