Home  >  Article  >  Backend Development  >  Detailed explanation of the goto statement added in PHP5.3

Detailed explanation of the goto statement added in PHP5.3

黄舟
黄舟Original
2017-06-25 13:33:082246browse

In "go" straight out of ASM or any other assembly language. Here's a link: I'd like to know: what can I do to make my code more efficient and how can I work on this larger project without snapping it up. Since goto statements allow you to jump back and forth and accidentally wait in an infinite loop if something happens the wrong way. Can you give me an example of this? EDIT: Well, I've seen the responses on CodeGo.net, and apparently there is widespread concern about "GOTO" and that it is bad. So I've been wondering: why is PHP's troublesome adding it to the language. If they don't see it inside, they won't do it...why? Also: A general comment about "go to" here in the StackOverflow EDIT2: Seeing as this question caused a lot of bad things it made me sad to go and ask my dad about the jump. He is 52 years old and an engineer. He did a good amount of programming in FORTRAN and COBOL during his days, mostly in FORTRAN and COBOL. Now he does IT services, servers and the like. Anyway, he said those things about "back in my day..." After a moment after that, he went back to saying that even back in his days as a student, they were shocked to know that this wasn't A clever idea, but they had no better idea of ​​the situation. TRY/CATCH is still a few years old and Error handling is almost existed. So what are your procedures for checking that you did? Add a few lines at the end that give you the printout and everything you need to check your code, and then you put the lines: "go to print;", or similar, to start the printing of your data. And in this way, you slowly debug your code. He agrees that going into the modern programming world what he finds reasonable is an "emergency interrupt" in debugging and unexpected situations. Sort of like goto fatal_error; and does something for the "is fatal_error" part of your code, showing you the in-depth results. But only if you create the final product there should be no jumps - Late edit: Another way to go about "go" in PHP5.3/PHP6

1. If you are writing good PHP Code , you should not need to jump. I think it's a bug they're adding it in because it just leads to lazy programming. See for a good one on adding this to PHP, and here's Stack Overflow,
2. I've only found out about goto: typing out nested loops. But most new languages ​​have to do this without goto anyway (break d80b5def5ed1be6e26d91c2709f14170 in PHP, or break 67be473e3e4c8b68286aa956d361c780 in Java, etc.). Go to the end of a function to clean up the part. But again, this is not in a garbage collected language. In other words, if you don't know whether you goto you shouldn't.
3. Is there such a thing as a jump. Maybe, just maybe, it's possible to get out of multiple nested loops, but you can already do "break 2" like this. Tags in Java also serve this purpose better than go. Maybe it's an exception with code writing when you need to jump to the end of a bunch at a time where one of them fails. But that just fixes crappy code to even crappier code.
4. It can be used for debugging purposes so you don't have to break out or refactor blocks of code just to temporarily change the worker threads.
5. In classic VB coding, using goto is very convenient for simulating 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
...这里是一种模拟的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"
 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 used to clear at the end of a function, although I Thinking you could make another function that could be called to do this in case of cleanup. To be honest, I had never been in PHP and I thought to myself: "Well, I wish there could be a goto. I didn't read up on why they decided to do this, but, those guys are very smart and had the opportunity to consider PHP Very good direction so far, so maybe there's a need for that, we don't know yet.
6. The thing I see about using goto is the cross-language port I wrote in C. The language 's parser Generator generates a parser with goto statements (it's easier to use goto than more sane control structures), and porting it to PHP is now less of a headache ##. #7. When writing finite state machines, you do need one of them when the parsing context is released, although we can live without a jump if
continue $case; is in a switch block. Efficiently jumping to a different situation and closing the scope is achievable in as many languages ​​as there are nowadays. Until then, we were pretty much stuck with goto 8. Jumps can help reduce code duplication in stack unwinding. Pseudocode 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;

(伪由罗伯特爱,从linux内核归档的邮件列表采取代码:) 
9. 由于之前已经说过,GOTO是唯一真正需要的类型的算法,通常是那些在语言解析或有限状态机。我从来没有错过在PHP缺乏goto语句。 OTOH,我有一个语言,其中只有两个结构是函数和条件goto方法:SNOBOL4。由于意大利面条代码的风险是如此之高,最SNOBOL4是/很小心的避免这一点。但是goto语句也使很紧的编程,创意循环执行等。它实际上是更容易做到的FSM型圈,如果你有在使用goto。 
10. 我承认我转到我的代码。 :) 唯一的理由似乎有利于在最短的flyway从其他语言到PHP(实际上只改变语言不碰控制结构)和重构代码的移植的第2阶段。 我个人认为和同事,因为他们可以避开有条件决裂-S的循环,他们将能够抵御转到诱惑。
11. 生成的代码可以使goto语句,我猜。对生成的代码的好处是,你并不需要去维护它-你只是重新生成它。 
12. goto真正应该尽管这是在语言,并会进行过时由于更好的编程实践。现在添加它似乎是一个倒退的一步。 
13. goto语句中的B大优势就是学习曲线。人们不禁要问,为什么像visualstudia和Mac电脑的工具做的很好。原因是,人们需要的不仅仅是一个伟大的产品,他们希望有一个 伟大的产品,他们可以在短短一个小时左右学习。现在很多 一个天只计划为他们的工作之一。我看到这么多的书说,一个人永远不能 使用goto语句,然后给五年左右的技术,例如,他们说每消除 需要它。我说,只是事实,5是该有多好转到证明 是!!我没有教的五件事,包括异常结构, 把整个章节来解释!!当你真正需要的是一个简单的跳转 可在30秒内进行解释。当然,你可以创建恶意代码与他们如果 希望---但是,嘿,最不想写糟糕的代码,如果他们 他们才可以呢。最goto语句在我们的实验室所做的代码简单 了解和学习;程度远远超过了读一本2000页的书。

The above is the detailed content of Detailed explanation of the goto statement added in PHP5.3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn