views:

510

answers:

6

I've heard rumors that PHP6 is planning on introducing a "goto" command. What is it supposed to be doing, once PHP6 gets out?

I've tried searching a bit, but haven't found anything awfully descriptive. I understand that it won't be a "GOTO 10"-like command...

+1  A: 

Granted, I am not a PHP programmer, and I don't know what PHP's exact implementation of GOTO will look like, but here is my understanding of GOTO:

GOTO is just a more explicit flow control statement like any other. Let's say you have some nested loops and you only need to find one thing. You can put in a conditional statement (or several) and when conditions are met properly, you can use a GOTO statement to get out of all the loops, (instead of having a 'break' statement at each level of nesting with a conditional statement for each. And yes, I believe the traditional implementation is to have named labels that the GOTO statement can jump to by name. You can do something like this:

for(...) {
    for (...) {
        for (...) {
        // some code
        if (x) GOTO outside;
        }
    }
} 
:outside

This is a simpler (and more efficient) implementation than without GOTO statements. The equivalent would be:

for(...) {
    for (...) {
        for (...) {
            // some code
            if (x) break;
        }
        if(x) break;
    }
    if(x) break;
}

In the second case (which is common practice) there are three conditional statements, which is obviously slower than just having one. So, for optimization/simplification reasons, you might want to use GOTO statements in tightly nested loops.

postfuturist
You don't need GOTO for this reason. See my answer for a more structured way to express breaking out of loops that always works and introduces zero inefficiency. Goto for this purpose is just dumb. (Not a reflection on you.)
Ira Baxter
A: 

It looks like it's currently in PHP 5.3, but is not fully documented yet. From what I can tell it shares its goto syntax with C, so it should be easy to pick up and use. Just remember Dijkstra's warning and use it only when necessary.

Kyle Cronin
+6  A: 

They are not adding a real GOTO, but extending the BREAK keyword to use static labels. Basically, it will be enhancing the ability to break out of switch nested if statements. Here's the concept example I found:

<?php
for ($i = 0; $i < 9; $i++)
{
        if (true) {
                break blah;
        }
        echo "not shown";
blah:
        echo "iteration $i\n";
}
?>

Of course, once the GOTO "rumor" was out, there was nothing to stop some evil guys to propagate an additional COMEFROM joke. Be on your toes.

See also:

http://www.php.net/~derick/meeting-notes.html#adding-goto

http://www.corephp.co.uk/archives/19-Prepare-for-PHP-6.html

Ishmaeel
I think that is truly confusing, because a break on its own will jump out of the for loop, not just the if statement.
DisgruntledGoat
A: 

@steveth45

My rule of thumb is that if you have nested code more than 3 levels deep, you are doing something wrong.

Then you don't have to worry about using multiple break statements or goto :D

FlySwat
I slightly agree but sometimes multiple levels of nested loops in unavoidable and shouldn't be scoffed at.
Christian
A: 

In the example given by steveth45 you can use a function instead:

function findItem(...) {
  for (...) {
    for (...) {
      for (...) {
        if (x) {
          return theItem;
        }
      }
    }
  }
}

// no need for label now
theItem = findItem(a, b, c);
grom
+1  A: 

I'm always astonished at how incredibly dumb the PHP designers are. If the purpose of using GOTOs is to make breaking out of multiply nested loops more efficient there's a better way: labelled code blocks and break statements that can reference labels: a: for (...) { b: for (...) { c: for (...) { ... break a; } } } Now is is clear which loop/block to exit, and the exit is structured; you can't get spaghetti code with this like you can with real gotos.

This is an old, old, old idea. Designing good control flow management structures has been solved since the 70s, and the literature on all this is long since written up. The Bohm-Jacopini theorem showed that you could code anything with function call, if-then-else, and while loops. In practice, to break out of deeply nested blocks, Bohm-Jacopini style coding required extra boolean flags ("set this flag to get out of the loop") which was clumsy coding wise and inefficient (you don't want such flags in your inner loop). With if-then-else, various loops (while,for) and break-to-labelled block, you can code any algorithm without no loss in efficiency. Why don't people read the literature, instead of copying what C did? Grrr.

Ira Baxter