search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the goto statement added in PHP5.3
Detailed explanation of the goto statement added in PHP5.3Jun 25, 2017 pm 01:33 PM
gotophpaboutjoin instatement

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 in PHP, or break 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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor