PHP 以及 PHP 5.3 中添加的 goto 语句

发布于 2024-07-16 01:33:50 字数 1304 浏览 3 评论 0原文

“goto”语句直接来自 ASM 或任何其他汇编语言。

这是一个链接:https://www.php.net/manual/ en/control-structs.goto.php

我想知道:这可以做什么让我的代码组织得更好? 我怎样才能在更大的项目中实现这一点,而不会把它搞砸。 由于 goto 允许您来回跳转,因此如果您以错误的方式使用它,就会发生意外赋值和无限循环。

有人可以给我一个很好使用这个的例子吗?

编辑:好吧,我已经看到了一些回复,显然对于“goto”语句的使用存在着广泛的共识,而且它很糟糕。

所以我仍然想知道:为什么 PHP 要把它添加到语言中。 如果他们没有看到其中的东西,他们就不会这样做……那为什么呢?

另外: StackOverflow 上关于“goto”的一般讨论

编辑2:看到这个问题引起了关于 goto 语句有很多不好的事情令人难过,我去问了我的父亲。 他今年 52 岁,是一名工业工程师。
他曾多次告诉我,他当时编写了大量的程序,而且大多使用 FORTRAN 和 COBOL。 现在他从事 IT 服务、服务器和网络管理等工作。

不管怎样,他说了一些关于“回到我那个时代……”的事情
讨论了一下之后,他回到了 goto 语句,说即使在他还是学生的时候,他们就已经知道使用它不是一个聪明的主意,但他们当时并没有更好的办法。 Try/catch 还需要几年的时间,错误处理几乎不存在。
那么你做了什么来检查你的程序呢? 在末尾添加几行,允许您打印输出以及在代码中签入所需的所有内容,然后放置行:“gotoprinting;”或类似的内容,以开始打印数据。

就这样,你逐渐调试了你的代码。

他同意 goto 在现代编程世界中的使用是毫无用处的。 他认为唯一合理的用途是“紧急中断”,用于极端调试和意外情况。 有点像goto fatal_error;,并让代码的“fatal_error”部分执行一些操作来向您显示深入的结果。
但仅限于创建某些内容期间。 成品不应该有 goto 语句。

最新编辑:关于 PHP5.3/PHP6 中“goto”的另一个讨论

The "goto" statement comes straight out of ASM or any other assembler language.

Here's a link: https://www.php.net/manual/en/control-structures.goto.php

I'm wondering: what can this do to make my code more well-organized? How can I implement this in larger projects, without screwing it up.
Since the goto will allow you to jump back and forth, accidental assignments and infinite loops are waiting to happen if you use this the wrong way.

Can someone give me an example of a good use of this?

EDIT: allright, I've seen some of the replies and apparently a wide consensus exists about the use of the "goto" statement and it being bad.

So I'm still wondering: why would PHP bother to add it to the language. If they didn't see something in it, they wouldn't do it... so why?

Also: A discussion here on StackOverflow about "goto" in general

EDIT2: Seeing as this question induced a lot of bad things to be sad about the goto statement, I went and asked my father. He's 52 years old and is an Industrial Engineer.
He told me a couple of times he did a good amount of programming in his days and mostly in FORTRAN and COBOL. Nowadays he does IT services, server&networkmanagment and such.

Anyways, he said some stuff about "back in my day..."
After discussing that a bit, he came back to the goto statement, saying that even back in his days as a student, they allready knew it wasn't a smart idea to use it, but they didn't have much better back then. Try/catch was still years away and error handling hardly excisted.
So what did you do to check your program? Add a few lines at the end that allow you to print output and everything you need to check in your code, and then you place the line: "goto printing;", or something like that, to start the printing of your data.

And in this manner, you gradually debugged your code.

He agrees that the use of goto in the modern programming world is pretty useless. The only use he finds justified is an "emergency break", to be used in extreme debugging and unexpected situations. Kinda like goto fatal_error;, and have the "fatal_error" part of your code do some things to show you in-depth results.
But only during the creation of something. A finished product should not have goto-statements.

LATE EDIT: Another discussion about "goto" in PHP5.3/PHP6

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

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

发布评论

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

评论(16

戏舞 2024-07-23 01:33:50

如果您正在编写良好的 PHP 代码,则不需要使用 goto。 我认为他们添加它是一个错误,因为它只会导致懒惰的编程。

请参阅

http://www.procata.com/ blog/archives/2004/07/29/goto-in-php/

对于将其添加到 PHP 以及堆栈溢出的详细评论,

GOTO 仍然被认为有害吗?

If you're writing good PHP code, you shouldn't need to use goto. I think it's a mistake that they're adding it in, as it just leads to lazy programming.

See

http://www.procata.com/blog/archives/2004/07/29/goto-in-php/

For a good commentary on the addition of this to PHP, and also, here on stack overflow,

GOTO still considered harmful?

陌若浮生 2024-07-23 01:33:50

我只发现 goto 的两个用途:

  1. 打破嵌套循环。 但大多数较新的语言都有一种无需 goto 即可执行此操作的机制(PHP 中的 breakbreak在Java等中)。
  2. 转到函数末尾的清理部分。 但同样,这在垃圾收集语言中通常没有用处。

换句话说,如果您不知道是否应该使用 goto 来执行某些操作,那么您就不应该使用。

I have only ever found two uses for goto:

  1. To break out of nested loops. But most newer languages have a mechanism to do this without goto anyway (break <number> in PHP, or break <loop label> in Java, etc.).
  2. To go to a cleanup section at the end of a function. But again, this isn't often useful in a garbage-collected language.

In other words, if you don't know whether you should use goto for something, you shouldn't.

小姐丶请自重 2024-07-23 01:33:50

我认为语言中 goto 的主要用途是跨语言移植的能力。 我用 C 语言编写了一个解析器生成器,它使用 goto 生成解析器(因为使用 goto 比实现更理智的控制结构更容易),现在将其移植到 PHP 并不是那么令人头疼。

The main use I see in having gotos in a language is the ability to port across languages. I wrote a parser generator in C that generated parsers with gotos (because it was easier to use gotos than to implement more sane control structures), and now porting it to PHP isn't as much of a headache.

别靠近我心 2024-07-23 01:33:50

goto 可以帮助减少堆栈展开的代码重复,伪代码如下:(

do A
if (error)
    goto out_a;
do B
if (error)
    goto out_b;
do C
if (error)
    goto out_c;
goto out;

out_c:
undo C

out_b:
undo B:

out_a:
undo A

out:
return ret;

Robert Love 的伪代码,取自 linux 内核存档邮件列表: https://lkml.org/lkml/2003/1/12/203)

goto can help reduce code duplication for stack unwinding, in pseudo code below:

do A
if (error)
    goto out_a;
do B
if (error)
    goto out_b;
do C
if (error)
    goto out_c;
goto out;

out_c:
undo C

out_b:
undo B:

out_a:
undo A

out:
return ret;

( Pseudo code by Robert Love, taken from the linux kernel archive mailing list: https://lkml.org/lkml/2003/1/12/203 )

只是一片海 2024-07-23 01:33:50

它可用于调试目的,因此您不必为了临时更改工作流程而注释或重构代码块。

It can be used for debugging purposes so you don't have to comment out or refactor blocks of code just to temporary change the workflow.

无敌元气妹 2024-07-23 01:33:50

在经典 VB 编码中,使用 goto 可以方便地模拟 try/catch 错误处理,如下所示:

Function MyFunction() as String

'-- start of error block
'
 On Error Goto Catch
   ' do something here that might cause an error
   MyFunction = "IT WORKED"
   Exit Function

 Catch:
   ' error occured - do something else
   MyFunction = Err.Description

 '
 '-- end of error block

End Function

...这里是模拟 try/catch/finally 的方法 ..

Function MyFunction() as String

'-- start of error block
'
 On Error Goto Catch
   ' do something here that might cause an error
   MyFunction = "IT WORKED"
   Goto Finally

 Catch:
   ' error occured - do something else
   MyFunction = Err.Description
   Err.Clear

 Finally:
   ' put your finally code here

 '
 '-- end of error block

End Function

它也很有用对于函数末尾的清理,尽管我想您可以考虑调用另一个函数来进行清理。

老实说,我在 PHP 中从来没有遇到过这样的情况:“嗯,我希望有一个 goto 语句”。 我还没有了解他们为什么决定这样做,但是,那些人非常聪明,并且到目前为止已经将 PHP 带到了非常好的方向,所以也许他们正在预料到我们尚未意识到的需求。

In Classic VB coding, use of goto is handy for emulating try/catch error handling like this:

Function MyFunction() as String

'-- start of error block
'
 On Error Goto Catch
   ' do something here that might cause an error
   MyFunction = "IT WORKED"
   Exit Function

 Catch:
   ' error occured - do something else
   MyFunction = Err.Description

 '
 '-- end of error block

End Function

... and here is a way to emulate the try/catch/finally ..

Function MyFunction() as String

'-- start of error block
'
 On Error Goto Catch
   ' do something here that might cause an error
   MyFunction = "IT WORKED"
   Goto Finally

 Catch:
   ' error occured - do something else
   MyFunction = Err.Description
   Err.Clear

 Finally:
   ' put your finally code here

 '
 '-- end of error block

End Function

It can also be useful for cleanup at the end of a function, although i suppose you could make a case that another function can be called to do that cleanup.

In all honesty, I have never had an occasion in PHP where I thought to myself 'hmm, I wish there was a goto statement'. I haven't read up on why they decided to do this, but, those guys are pretty smart, and have taken PHP into very good directions so far, so maybe the are anticipating a need that we don't realize yet.

固执像三岁 2024-07-23 01:33:50

goto 没有什么好用的。

也许,只是也许,摆脱多个嵌套循环可能很有用,但您已经可以使用“break 2”等来做到这一点。 对于这个目的,像 Java 中的带标签的中断比 goto 更好。

也许它对于不使用异常编写的代码也很有用,当您需要在一个语句失败时跳到一堆语句的末尾时。 但这只是用更糟糕的代码来修复糟糕的代码。

There is no such thing as good use of goto.

Maybe, just maybe, it could be useful to get out of multiple nested loops, but you can already do that using "break 2" and such. Labeled breaks like in Java would be better than goto for this purpose.

Perhaps it's also useful with code written without using exceptions, when you need to skip to the end of a bunch of statements once one fails. But that's only fixing crappy code with more crappy code.

很快妥协 2024-07-23 01:33:50

我承认我从未在代码中使用过 goto。 :)

对我来说唯一的原因似乎是促进从其他语言到 PHP 的最短迁移路线(实际上只改变语言而不触及控制结构)并在移植的第二阶段重构代码。

就我个人而言,我相信受过教育的同事,因为他们可以避免循环中的条件中断,所以他们将能够抵制 goto 的诱惑。

I admit I have never used goto in my codes. :)

The only reason for me seems to facilitate the shortest migration route from other languages to PHP (practically only changing the language without touching the control structures) and refactor the code on the 2nd stage of the porting.

Personally I believe in educated colleagues and as they can avoid the conditional break-s from loops, they would be able to resist the temptation of goto.

眉目亦如画i 2024-07-23 01:33:50

Goto 主要在编写有限状态机时使用。 在解析上下文无关语法时,您实际上需要其中之一。 不过,如果 continue $case; 是 switch 块中跳转到不同 case 的有效语句,并且当然具有当今许多语言所具有的 case 范围,那么我们可以不用 goto 来生活。 在那之前我们几乎都被 goto 困住了。

Goto is primarily used when writing finite state machines. When parsing context free grammer you will actually need one of those. Though we could live without goto if continue $case; is a valid statement within a switch block to jump to a different case and off course having case ranges as many languages nowadays have. Until then we are pretty much stuck with goto.

清君侧 2024-07-23 01:33:50

有时我使用goto来避免多个嵌套的if。 这不仅涉及逻辑、结构或程序流程,有时还涉及代码的外观。

Sometimes I use goto to avoid multiple nested ifs. That's not only about the logic, the structure or the program flow, sometimes it can be just about how the code looks like.

明媚如初 2024-07-23 01:33:50

我猜生成的代码可以很好地利用 goto。 生成代码的好处是您不需要维护它 - 您只需重新生成它。

Generated code could make good use of goto, I guess. The good thing about generated code is that you don't need to maintain it - you just regenerate it.

自由范儿 2024-07-23 01:33:50

goto 确实应该是某种语言中的东西,并且由于更好的编程实践而将被淘汰。 现在添加它似乎确实是一种倒退。

goto should really be something though that was in the language and would be being made obsolete due to better programming practises. Adding it now does seem like a backwards step.

倾城月光淡如水﹏ 2024-07-23 01:33:50

gotos 的一大优点是学习曲线。 人们想知道为什么像 Visual Studia 和 Mac 这样的工具表现良好。 原因是人们想要的不仅仅是一个好的产品;还需要一个好的产品。 他们想要一个
很棒的产品,他们只需一个小时左右就能学会使用。 现在很多程序员
一天的节目作为他们的工作之一。 我看到很多书都说一个人永远不应该
使用 goto,然后给出五种左右的技术,他们说消除了每一种
需要它。 我说他们提到 5 的事实就证明了 goto 有多好
是!!!!! 我没有时间教授五件事,其中包括异常结构
用了整整一章来解释!!!! 当你真正需要的只是一个简单的 goto 时
可以在 30 秒内解释清楚。 当然,如果
程序员想要——但是,嘿,大多数程序员不想写出糟糕的代码,如果他们
无论如何,他们能做到吗? 我们实验室中的大多数 goto 使代码变得非常简单
理解和学习; 比阅读一本2000页的书要重要得多。

The b big advantage of gotos is learning curve. One wonders why tools like visual studia and macs do well. The reason is that people want more than a great product; they want a
great product that they can learn to use in just an hour or so. Many programmers now
a days only program as one of their jobs. I see so many books say that one should never
use a goto and then give five or so technologies such that they say eliminate every
need of it. I say that just that fact that they mentioned 5 is proof of how good the goto
is!!!!! I don't have time to teach five things that include exception structures that
take whole chapters to explain!!!!! when all you really need is a simple goto that
can be explained in 30 seconds. Sure, you can create bad code with them if the
programmer wants--- but hey, most programmers don't want to write bad code and if they
did they could anyway. Most gotos in our lab made the code extremely simple to
understand and learn; much more so than reading a 2000 page book.

我爱人 2024-07-23 01:33:50

GOTO,受限执行控制结构,可以用来代替循环,但强烈建议不要这样做。 它的使用往往会鼓励非结构化代码的创建,这是一种糟糕的做法。 如果仅在开发、调试(跳过大量代码来访问特定问题区域)和测试中使用,它很可能是最好的。 GOTO 的唯一其他目的可能是编写汇编程序; 不见得。 如果在开发之外使用 GOTO,则应谨慎使用,并且仅作为最后的手段。 如果可能,请用适用的循环结构替换 GOTO。

至于最后链接的线程(PHP 中的 GOTO 命令?):

正如 Ishmaeel 所说(已编辑作者:Gordon;迄今为止最好的答案):

GOTO 只是一个扩展的 BREAK,具有“使用静态标签”的能力。
“基本上,它将增强突破嵌套 if 语句的能力。”

GOTO, the restricted execution control structure, can be used in the place of loops, but that is highly discouraged. Its use tends to encourage the creation of unstructured code, which is a terrible practice. It is most likely best if used only in development, for debugging (skip large amounts of code to access a particular problem area) and testing. The only other purpose for the GOTO is possibly for writing an assembler; not likely. GOTO, if used outside of development, should be used sparingly and only as a last resort. If possible, replace a GOTO with an applicable loop structure.

As for the thread linked last (GOTO command in PHP?):

As stated by Ishmaeel (edited by Gordon; by far the best answer):

The GOTO is simply an extended BREAK, with the ability to "use static labels".
"Basically, it will be enhancing the ability to break out of nested if statements."

三人与歌 2024-07-23 01:33:50

简而言之,goto 是有限堆栈空间的一种解决方法,在单线程代码中具有更好的性能。 除了解决堆栈空间或性能问题之外,它的使用至少是不必要的,最多是不合适的,因为它会导致不必要的复杂性。

我用所有语言编写了超过 200 万行代码,不包括机器代码:-)。 只有两次需要使用它,并且都是由于检查\排序大型树数据集。

The short answer is goto is a workaround for a limited stack space with much better performance in single threaded code. Other than addressing stack space or performance, its usage would be at least unnecessary and at most inappropriate because it causes unneeded complexity.

In over 2 million lines of code I've written in all languages, excluding machine code :-). Only twice has its use been necessary and both due to inspecting\sorting large tree datasets.

梦晓ヶ微光ヅ倾城 2024-07-23 01:33:50

正如之前所说,goto 仅在某些类型的算法中才真正需要,通常是在语言解析或有限状态机中出现的算法。 我从来没有错过 PHP 中缺少 goto 的问题。

OTOH,我用一种语言编程,其中唯一的两种结构是函数和条件 goto: SNOBOL4 。 由于意大利面条代码的风险如此之高,大多数 SNOBOL4 程序员都小心地避免这种情况。 但 goto 确实实现了一些非常紧凑的编程、创造性的循环执行等等。 如果您拥有的只是 goto,那么实际上执行 FSM 类型的循环会更容易一些。

As has been said before, goto is only really required in some types of algorithms, usually those that come up in language parsing or finite state machines. I have never missed the lack of goto in PHP.

OTOH, I have programmed in a language where the only two structures were functions and conditional gotos: SNOBOL4. Since the risk of spaghetti code was so high, most SNOBOL4 programmers were/are careful to avoid that. But gotos did enable some very tight programming, creative loop executions and so on. It's actually somewhat easier to do FSM-type loops if all you have are gotos.

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