<p>I've heard rumors that PHP is planning on introducing a "goto" command. What is it supposed to be doing?</p>

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

[{'answer_id': 19398, 'author': 'postfuturist', 'author_id': 1892, 'author_profile': 'https://Stackoverflow.com/users/1892', 'pm_score': 2, 'selected': False, 'text': "<p>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:</p>\n\n<p>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:</p>\n\n<pre><code>for(...) {\n    for (...) {\n        for (...) {\n        // some code\n        if (x) GOTO outside;\n        }\n    }\n} \n:outside\n</code></pre>\n\n<p>This is a simpler (and more efficient) implementation than without GOTO statements. The equivalent would be:</p>\n\n<pre><code>for(...) {\n    for (...) {\n        for (...) {\n            // some code\n            if (x) break;\n        }\n        if(x) break;\n    }\n    if(x) break;\n} \n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 19402, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 0, 'selected': False, 'text': '<p>It <a href="http://wiki.php.net/doc/todo/undocumented#php_5.3" rel="nofollow noreferrer">looks</a> 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 <a href="http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF" rel="nofollow noreferrer">warning</a> and use it only when necessary.</p>\n'}, {'answer_id': 19403, 'author': 'Ishmaeel', 'author_id': 227, 'author_profile': 'https://Stackoverflow.com/users/227', 'pm_score': 4, 'selected': True, 'text': '<p>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 <s>switch</s> nested if statements. Here\'s the concept example I found:</p>\n\n<pre><code>&lt;?php\nfor ($i = 0; $i &lt; 9; $i++) {\n    if (true) {\n        break blah;\n    }\n    echo "not shown";\n    blah:\n    echo "iteration $i\\n";\n}\n?&gt;\n</code></pre>\n\n<p>Of course, once the GOTO "rumor" was out, there was nothing to stop some evil guys to propagate an additional <strong>COMEFROM</strong> joke. Be on your toes.</p>\n\n<p>See also:</p>\n\n<p><a href="http://www.php.net/~derick/meeting-notes.html#adding-goto" rel="nofollow noreferrer">http://www.php.net/~derick/meeting-notes.html#adding-goto</a></p>\n'}, {'answer_id': 19404, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 0, 'selected': False, 'text': "<p>@steveth45</p>\n\n<p>My rule of thumb is that if you have nested code more than 3 levels deep, you are doing \nsomething wrong.</p>\n\n<p>Then you don't have to worry about using multiple break statements or goto :D</p>\n"}, {'answer_id': 19452, 'author': 'grom', 'author_id': 486, 'author_profile': 'https://Stackoverflow.com/users/486', 'pm_score': 1, 'selected': False, 'text': '<p>In the example given by <a href="https://stackoverflow.com/questions/19388/goto-command-in-php6#19398">steveth45</a> you can use a function instead:</p>\n\n<pre><code>function findItem(...) {\n  for (...) {\n    for (...) {\n      for (...) {\n        if (x) {\n          return theItem;\n        }\n      }\n    }\n  }\n}\n\n// no need for label now\ntheItem = findItem(a, b, c);\n</code></pre>\n'}, {'answer_id': 1078480, 'author': 'Ira Baxter', 'author_id': 120163, 'author_profile': 'https://Stackoverflow.com/users/120163', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m always astonished at how incredibly dumb the PHP designers are.\nIf the purpose of using GOTOs is to make breaking out of multiply nested\nloops more efficient there\'s a better way:   labelled code blocks\nand break statements that can reference labels:</p>\n\n<pre><code>a:  for (...) {\n    b:  for (...) {\n         c: for (...) {\n               ...\n               break a;\n           }\n       }\n   }\n</code></pre>\n\n<p>Now is is clear which loop/block to exit, and the exit is structured;\nyou can\'t get spaghetti code with this like you can with real gotos.</p>\n\n<p>This is an old, old, old idea.  Designing good control flow management\nstructures has been solved since the 70s, and the literature on all this\nis long since written up.   The Bohm-Jacopini theorem showed that\nyou could code anything with function call, if-then-else, and while loops.\nIn practice, to break out of deeply nested blocks, Bohm-Jacopini style\ncoding required extra boolean flags ("set this flag to get out of the loop")\nwhich was clumsy coding wise and inefficient (you don\'t want such flags\nin your inner loop). With if-then-else, various loops (while,for)\nand break-to-labelled block, you can code any algorithm without no\nloss in efficiency.  Why don\'t people read the literature, instead\nof copying what C did? Grrr.</p>\n'}, {'answer_id': 16190143, 'author': 'Jurijs Nesterovs', 'author_id': 2181738, 'author_profile': 'https://Stackoverflow.com/users/2181738', 'pm_score': 0, 'selected': False, 'text': '<p>there is a <code>goto</code> in php -> <a href="http://php.net/manual/en/control-structures.goto.php" rel="nofollow">http://php.net/manual/en/control-structures.goto.php</a>, but i wouldn\'t use it, just write normal code...</p>\n'}]
['https://Stackoverflow.com/questions/19388', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2238/']