简体   繁体   English

PHP中的GOTO命令?

[英]GOTO command in PHP?

I've heard rumors that PHP is planning on introducing a "goto" command. 我听说有传言称PHP正计划引入“goto”命令。 What is it supposed to be doing? 该怎么办?

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... 我明白它不会是一个“ GOTO 10 ”式的命令......

They are not adding a real GOTO, but extending the BREAK keyword to use static labels. 他们没有添加真正的GOTO,而是扩展BREAK关键字以使用静态标签。 Basically, it will be enhancing the ability to break out of switch nested if statements. 基本上,它将增强断开switch嵌套if语句的能力。 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. 当然,一旦GOTO“谣言”消失,就没有什么可以阻止一些邪恶的家伙传播额外的COMEFROM笑话。 Be on your toes. 做你的脚趾。

See also: 也可以看看:

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

I'm always astonished at how incredibly dumb the PHP designers are. 我总是对PHP设计师的愚蠢程度感到惊讶。 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: 如果使用GOTO的目的是为了使多重嵌套循环更有效率,那么有一种更好的方法:标记代码块和可以引用标签的break语句:

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. 自70年代以来,设计良好的控制流程管理结构已经得到了解决,所有这些的文献早已写完。 The Bohm-Jacopini theorem showed that you could code anything with function call, if-then-else, and while loops. Bohm-Jacopini定理表明你可以使用函数调用,if-then-else和while循环来编写任何代码。 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). 在实践中,为了打破深层嵌套的块,Bohm-Jacopini样式编码需要额外的布尔标志(“设置此标志以摆脱循环”)这是笨拙的编码明智和低效(你不希望这样的标志在你的内环)。 With if-then-else, various loops (while,for) and break-to-labelled block, you can code any algorithm without no loss in efficiency. 使用if-then-else,各种循环(while,for)和break-to-labeled块,您可以编写任何算法而不会降低效率。 Why don't people read the literature, instead of copying what C did? 人们为什么不阅读文献,而不是复制C所做的事情? Grrr. 哎呀。

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: 当然,我不是一个PHP程序员,我不知道PHP的GOTO的确切实现是什么样的,但这是我对GOTO的理解:

GOTO is just a more explicit flow control statement like any other. GOTO只是一个比任何其他更明确的流控制语句。 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: 你可以放入一个条件语句(或几个),当条件得到正确满足时,你可以使用GOTO语句来摆脱所有循环,(而不是在每个嵌套级别使用条件语句都有'break'语句对于每一个。是的,我相信传统的实现是使用GOTO语句可以按名称跳转的命名标签。你可以这样做:

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

This is a simpler (and more efficient) implementation than without GOTO statements. 与没有GOTO语句相比,这是一种更简单(也更有效)的实现。 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. 因此,出于优化/简化的原因,您可能希望在紧密嵌套的循环中使用GOTO语句。

In the example given by steveth45 you can use a function instead: steveth45给出的示例中,您可以使用函数:

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

// no need for label now
theItem = findItem(a, b, c);

在php中有一个goto - > http://php.net/manual/en/control-structures.goto.php ,但我不会用它,只需编写正常的代码......

It looks like it's currently in PHP 5.3, but is not fully documented yet. 看起来它目前在PHP 5.3中,但尚未完全记录。 From what I can tell it shares its goto syntax with C, so it should be easy to pick up and use. 从我可以告诉它与C共享它的goto语法,所以它应该很容易上手和使用。 Just remember Dijkstra's warning and use it only when necessary. 只需记住Dijkstra的警告,并在必要时使用它。

@steveth45 @ steveth45

My rule of thumb is that if you have nested code more than 3 levels deep, you are doing something wrong. 我的经验法则是,如果嵌套的代码超过3级,那么你做错了。

Then you don't have to worry about using multiple break statements or goto :D 然后你不必担心使用多个break语句或转到:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM