In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the application of goto grammar in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The use of goto Syntax in PHP
In C++, Java, and many languages, there is a magical grammar, which is goto. As the name implies, its use is to go directly to a place. From a code point of view, that is, jump directly to the specified place. We also have this feature in our PHP, so let's take a look at how it works:
Goto a
Echo "1"; / / will not output
A:
Echo '2percent; / / 2
When the code runs to the goto location, it jumps to the line of code where a: is located and continues. It's fun, but this feature is useful for complex nested if or for jumping out of loops, especially for handling some exception or error situations, such as:
For ($I = 0, $j = 50; $I < 100; $iTunes +) {
While ($jmurt -) {
If ($jacks 17) {/ / suppose $jacks 17 is an abnormal condition
Goto end; / / jumps away directly, and the result of the end of the loop is not output.
}
}
}
Echo "I = $I"
End:
Echo'j hit 174th; / / output or handle exception cases directly here
It feels good, doesn't it, but there are some limitations to goto syntax:
The target location can only be located in the same file and scope, that is, you cannot jump out of a function or class method, nor jump into another function. You cannot jump into any loop or switch structure to jump out of a loop or switch. The usual usage is to use goto instead of multi-tier break.
For example, the following code is invalid:
$a = 1
Goto switchgo
Switch ($a) {
Case 1:
Echo 'bb'
Break
Case 2:
Echo 'cc'
Switchgo:
Echo "bb"
Break
}
Goto whilego
While ($a < 10) {
$asides +
Whilego:
Echo $a
}
/ / Fatal error: 'goto' to undefined label' ifgo'
They all report the same error because the defined goto tag cannot be found because of the scope. It is also important to note what might cause a dead loop when using goto, as follows:
B:
Echo 'b'
Goto b
When the code executes to goto, it jumps back to the previous b tag line, then continues to execute, and then it comes to goto, which becomes an endless loop. It feels a bit like while (true). However, there is no break in this goto loop, so you can only go somewhere else in the goto.
Therefore, the use of goto syntax is very small, because it can disrupt the logical flow of your code, but people who like it will feel that it can make the code very flexible. This depends on the choice of the wise and the benevolent. At present, the use of this syntax is not very advocated in most language documents, including PHP. My advice is that if it is not a very special situation or to show off skills, try not to use Goto syntax, when the project code becomes complex, it is easy to confuse others or yourself.
Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/goto%E8%AF%AD%E6%B3%95%E5%9C%A8PHP%E4%B8%AD%E7%9A%84%E4%BD%BF%E7%94%A8.md
Reference document: https://www.php.net/manual/zh/control-structures.goto.php
This is the end of the content of "the Application of goto Grammar in PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.