PHP 中的 GOTO 命令?

发布于 2024-07-04 09:13:47 字数 122 浏览 11 评论 0原文

我听说 PHP 计划引入“goto”命令的传言。 它应该做什么?

我尝试搜索了一下,但没有找到任何具有描述性的内容。 我知道它不会是一个类似“GOTO 10”的命令......

I've heard rumors that PHP is planning on introducing a "goto" command. 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

街角迷惘 2024-07-11 09:13:48

steveth45 给出的示例中,您可以使用函数代替:

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

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

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);
弥枳 2024-07-11 09:13:48

当然,我不是 PHP 程序员,我不知道 PHP 的 GOTO 的具体实现是什么样的,但这是我对 GOTO 的理解:

GOTO 只是一个像其他任何语句一样更明确的流程控制语句。 假设您有一些嵌套循环,并且您只需要找到一件事。 您可以放入一个(或多个)条件语句,当条件正确满足时,您可以使用 GOTO 语句退出所有循环(而不是在带有条件语句的每个嵌套级别都使用“break”语句是的,我相信传统的实现是有 GOTO 语句可以按名称跳转到的命名标签,您可以执行以下操作:

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

这是比没有 GOTO 语句更简单(且更有效)的实现。是:

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

在第二种情况(这是常见的做法)中,存在三个条件语句,这显然比只有一个条件语句慢。因此,出于优化/简化的原因,您可能希望在紧密嵌套的循环中使用 GOTO 语句。

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.

晌融 2024-07-11 09:13:48

我总是对 PHP 设计者的愚蠢程度感到惊讶。
如果使用 GOTO 的目的是为了打破多重嵌套
循环更高效有一个更好的方法:标记代码块
和可以引用标签的break语句:

a:  for (...) {
    b:  for (...) {
         c: for (...) {
               ...
               break a;
           }
       }
   }

现在很清楚要退出哪个循环/块,并且退出是结构化的;
你不能像用真正的 goto 那样得到意大利面条代码。

这是一个古老的、古老的、古老的想法。 设计良好的控制流管理
自 70 年代以来,结构问题就已得到解决,有关这一切的文献
已经写很久了。 玻姆-雅可比尼定理表明
您可以使用函数调用、if-then-else 和 while 循环编写任何代码。
在实践中,打破深度嵌套的块,Bohm-Jacopini 风格
编码需要额外的布尔标志(“设置此标志以退出循环”)
这是笨拙的编码方式并且效率低下(你不想要这样的标志
在你的内循环中)。 使用 if-then-else,各种循环(while、for)
和中断到标记块,您可以编写任何算法,而无需
效率损失。 为什么人们不去读文献,而是去读文献?
复制 C 所做的事情? 咕噜。

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.

丢了幸福的猪 2024-07-11 09:13:48

php中有一个goto -> http://php.net/manual/en/control-structs.goto.php ,但我不会使用它,只需编写普通代码......

there is a goto in php -> http://php.net/manual/en/control-structures.goto.php, but i wouldn't use it, just write normal code...

旧人 2024-07-11 09:13:48

@steveth45

我的经验法则是,如果您的嵌套代码深度超过 3 层,那么您正在做的
有事吗。

那么你就不必担心使用多个break语句或goto:D

@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

彼岸花似海 2024-07-11 09:13:48

看起来目前在 PHP 5.3 中,但没有完整记录然而。 据我所知,它与 C 共享其 goto 语法,因此它应该很容易学习和使用。 只需记住 Dijkstra 的警告并仅在必要时使用它。

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.

错々过的事 2024-07-11 09:13:47

他们没有添加真正的 GOTO,而是扩展了 BREAK 关键字以使用静态标签。 基本上,它将增强打破 switch 嵌套 if 语句的能力。 这是我找到的概念示例:

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

当然,一旦GOTO“谣言”传出,就没有什么可以阻止一些邪恶的人再传播一个COMEFROM笑话。 保持警惕。

另请参阅:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文