Detailed explanation of the pros and cons of php goto statement
You can tell at a glance that he is a kid who can’t tell the difference between goto and call
Then when he was typing code in the group, the kitten used Do...Looploop, but a child in the group asked Do... What is a loop? -_-|||As a result, a group of people said that they rarely use Do...Loop loops, and most of them like to use Goto. I discovered this problem. It does not mean that Goto cannot be used, but that Goto is used less! ! !
The nonsense is over, let’s get down to business
First explain the difference between Goto and Call for novices
Goto is used to jump to the code, that is, when the code is executed to Goto , jump to the position specified by Rem and execute downward. It will not return to the next statement of the Goto statement when it is completed.
Call is also used to jump codes, but to be more precise, Call is used to call subroutines! What is a call? That is, when the Call is executed, the specified subroutine (Sub) is placed in the code. When the subroutine is executed, it will return to the next line of the Call statement and continue to execute downwards.
If it is difficult to understand, then use the code of the classmate mentioned at the beginning to explain:
This is the Goto statement |
This is the Call statement |
1. a=15 2. b=10 3. if a-b < 15 4. goto sub1//跳转至Rem sub1处,且不会再跳回来了!!! 5. if a-b > -15 6. goto sub2 7. end if 8. end if 9. 10. Rem sub2 11. MsgBox "a-b大于-15" 12. 13. Rem sub1 14. MsgBox "a-b小于15"//脚本到底部了,执行结束!!! |
1. a=15 2. b=10 3. if a-b < 15 4. call sub1//调用sub1子程序,当子程序执行结束后,返回到此处,继续向下执行 5. if a-b > -15 6. call sub2 7. end if 8. end if 9. 10. Sub sub2() 11. MsgBox "a-b大于-15" 12. End Sub 13. 14. Sub sub1() 15. MsgBox "a-b小于15" 16. End Sub |
OK, the two differences have been clearly explained. Now let’s talk about this topic, the pros and cons of the Goto statement! ! !
As I said above, I advocate that Goto should be used less and must not be abused. One of the main reasons is that the Goto statement destroys the program structure and makes the program less readable. If you use one, Two may not have much impact, but what if you use more than a dozen Gotos?
The following paragraph is taken from Baidu Encyclopedia (if you think it is too much, then you only need to read the "Results of the goto statement" section)
Origin of the problem:
After the mid-1960s, computer hardware technology has improved day by day. The storage capacity, computing speed and reliability of computing have increased significantly, and the cost of producing hardware has continued to decrease. The decline in computer prices has created excellent conditions for its widespread application. In this situation, there is an urgent need for computer software to adapt to it. Therefore, some requirements for developing large-scale software systems have been put forward. However, the progress of software technology has not been able to meet the needs of the development of the situation. In the development process of large-scale software, three major problems have arisen: high complexity, long development cycle, and difficulty in ensuring accuracy. There is no solution to the problems encountered, which causes problems to pile up and form a situation that is difficult for people to control. The so-called "software crisis" occurs. In order to overcome this crisis, on the one hand, it is necessary to conduct a series of research on issues such as programming methods, program correctness and software reliability; on the other hand, it is also necessary to conduct research on methods of software preparation, testing, maintenance and management. , thus producing programming methodology.
The view that goto statements are harmful:
In 1968, E.W. Dykstra first put forward the argument that "GOTO statements are harmful", pointing to the traditional Programming methods pose challenges that have drawn widespread attention to the discussion of programming methods.
Controversy over the goto statement:
In the late 1960s and early 1970s, the debate over the usage of the GOTO statement was fierce. People who advocate removing the GOTO statement from high-level programming languages believe that the GOTO statement is a harmful statement that has the greatest impact on the program structure. Their main reason is that the GOTO statement makes the static structure and dynamic structure of the program Inconsistency makes the program difficult to understand and troubleshoot. After removing the GOTO statement, the program running process can be reflected directly from the program structure. In this way, it not only makes the program structure clear, easy to understand and error-checking, but also helps to prove the correctness of the program.
Opponents believe that the GOTO statement is more flexible to use, and in some cases it can improve the efficiency of the program. If the GOTO statement is completely deleted, in some cases it will make the program too complex and increase unnecessary calculations.
About the solution to the goto statement:
In 1974, D.E. Knuth made a comprehensive and fair review of the GOTO statement controversy. His basic point is : Using the GOTO statement without restrictions, especially the GOTO statement that jumps back, will cause the program structure to change. It is difficult to understand. In this case, you should try to avoid using the GOTO statement. But in other cases, in order to improve the efficiency of the program without destroying the good structure of the program, it is necessary to use some GOTO statements in a controlled manner. In his words: "In some cases, I advocate deleting the GOTO statement; in other cases, I advocate the introduction of the GOTO statement." Since then, this 10-year-old debate has been settled.
Later, G. Giacoppini and C. Boehm theoretically proved that any program can be expressed using sequence, branching and repeating structures. This conclusion shows that removing the GOTO statement from a high-level programming language does not affect the programming ability of the high-level programming language, and the structure of the written program is clearer. [/hide]
gotoThe result of the statement:
The goto statement is retained in advanced programming languages such as C/C++, but it is recommended not to be used or to be used sparingly. In some updated high-level programming languages, such as Java, which does not provide goto statements, although it specifies goto as a keyword, it does not support its use, making the program concise and easy to read; nevertheless, later c# still supports goto For statements, one advantage of the goto statement is that it can ensure that the program has a unique exit and avoid too large if nesting.
如果有仔细看上面这段话的同学,相信能明白goto语句的利与弊了吧?
好吧,还是用代码来解释:
滥用goto语句的例子 |
合理利用goto语句 |
1. Rem A 2. If false 3. goto B 4. End If 5. 6. Rem C 7. if true 8. goto A 9. ElseIf false 10. goto B 11. else 12. goto C 13. End If 14. 15. Rem B 16. //这样的代码可读吗,你可以划出流程图吗。而且goto完全是可以用其他语句代替的。 |
1. Do 2. Do 3. Do 4. If true 5. Goto 停止//利用goto跳出深层嵌套 6. End If 7. Loop 8. Loop 9. Loop 10. 11. Rem 停止 |
讲解就到这里了,小猫的想法就是建议大家如果能不用goto就不用goto,尽量多使用Call,如果想跳出循环,每种循环都有对应的跳出语句,如exit do,exit for,exit sub,Exit Function
另外当大家需要用到循环时,建议使用Do...Loop和For...Next两者,而while循环比较绕口,完全可以用do和for代替他
The above is the detailed content of Detailed explanation of the pros and cons of php goto statement. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment