当前位置:网站首页>The method of parsing PHP to jump out of the loop and the difference between continue, break and exit
The method of parsing PHP to jump out of the loop and the difference between continue, break and exit
2022-07-07 14:33:00 【Full stack programmer webmaster】
This article is about PHP The method of jumping out of the loop and continue、break、exit The differences between them are analyzed and introduced in detail , Friends in need of reference
PHP The circular structure in is roughly for loop ,while loop ,do{} while Cycles and foreach Several kinds of circulation , In either cycle , stay PHP There are several ways to jump out of the loop : Code : The code is as follows :
<?php
$i = 1;
while (true) { // It seems that this loop will always execute
if ($i==2) {
// 2 Skip not show
$i++;
continue;
} else if ($i==5) {
// But here $i=5 Just jump out of the cycle
break;
} else {
echo $i . '<br>';
}
$i++;
}
exit;
echo ' No output here ';
?>result :
1 3 4 continuecontinue It is used in the loop structure , The control program abandons this cycle continue Statement and move on to the next loop .continue Itself does not jump out of the loop structure , Just give up this cycle . If in an acyclic structure ( for example if In the sentence ,switch In the sentence ) Use continue, The program will go wrong . For example, in the following paragraph PHP In the code snippet :
The code is as follows :
<?php
for($i = 1;$i <= 100; $i++ ){
if($i % 3 == 0 || $i % 7 == 0){
continue;
}
& #160; else{
echo”$i \n<br/>”;
}
}
?>PHP The function of the code snippet of is to output 100 within , Can neither be 7 It can't be divided 3 Those natural numbers divisible , In the cycle, use if Conditional statements judge numbers that can be divisible , And then execute continue; sentence , It goes directly to the next cycle . The following output statement will not be executed .
breakbreak It is used in the above mentioned cycles and switch Statement . His function is to jump out of the current grammatical structure , Execute the following statement .break Statement can take a parameter n, Indicates the number of layers to jump out of the loop , If you want to jump out of multiple loops , It can be used n To indicate the number of layers to jump out , If there are no parameters, the default is to jump out of the loop . Look at the following example of multiple loop nesting :
The code is as follows :
<?php
for ($i = 1; $i <= 10; $i++) {
for ($i = 1; $i <= 10; $i++) {
for ($j = 1; $j <= 10; $j++) {
$m = $i * $i + $j * $j;
echo”$m \n < br />”;
if ($m < 90 || $m > 190) {
break 2;
}
}
}
}
?>It's used here break 2 Out of the double cycle , You can experiment , take 2 Get rid of , The result is completely different . If you do not use parameters , What jumps out is only this cycle , The first level of loop will continue .
goto goto It's actually just an operator , Like any other language ,PHP Abuse is also discouraged goto, The abuse of goto It will cause the readability of the program to decline seriously .goto The function of is to jump the execution of the program from the current position to any other position ,goto There is no function of the cycle to end in itself , But the function of its jump position makes it can be used as a jump cycle . but PHP5.3 And above versions have stopped on goto Support for , So try to avoid using goto. The following one uses goto Examples of jumping out of a loop
The code is as follows :
for($i = 1000;$i >= 1 ; $i– ){ if( sqrt($i) <= 29){ goto a; } echo “$i”; } a: echo” this is the end”;
The example uses goto To jump out of the loop , This example is used to detect 1000 within , The square root of those numbers is greater than 29. exit exit It is used to end program execution . It can be used anywhere , There is no meaning of jumping out of the loop .exit It can take a parameter , If the argument is a string ,PHP The string will be output directly , If the parameter is integer plastic ( The scope is 0-254), That parameter will be used as the end state .
The code is as follows :
<?php
for($i = 1000;$i >= 1 ; $i– ){
if( sqrt($i) >= 29){
echo”$i \n<br/>”;
}
else{
exit;
}
}
echo” This line will not be exported ”;
?>In the above example, the code runs directly from the loop , This will lead to the following code will not be executed , If it's in a php web Inside the page , Even exit hinder html The code will not be output .
return return Statement is used to end a piece of code , And return a parameter . Can be called from a function , You can also choose from one include() perhaps require() Statement to call , It can also be called in the main program , If it is called in a function, the program will immediately end running and return parameters , If it is include() perhaps require() Statement is called in the file contained by , Program execution will immediately return to the program that called the file , The return value will be taken as include() perhaps require() The return value of . And if it is called in the main program , Then the main program will stop executing immediately
The code is as follows :
<?php
for($i = 1000;$i >= 1 ; $i– ){
if( sqrt($i) >= 29){
echo”$i \n<br/>”;
}
else{
return;
}
}
echo” This line will not be exported ”;
?>The example here and the above use exit The effect is the same .
At the end of the cycle , Jump out naturally Of course, this is the best understanding , When the cycle meets the critical condition of the cycle, it is self exiting . Above is PHP A simple summary of several ways to jump out of the loop .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/113261.html Link to the original text :https://javaforall.cn
边栏推荐
- Selenium Library
- PD虚拟机教程:如何在ParallelsDesktop虚拟机中设置可使用的快捷键?
- The longest ascending subsequence model acwing 1014 Mountaineering
- Substance painter notes: settings for multi display and multi-resolution displays
- Mrs offline data analysis: process OBS data through Flink job
- Nllb-200: meta open source new model, which can translate 200 languages
- 一个程序员的水平能差到什么程度?尼玛,都是人才呀...
- Es log error appreciation -maximum shards open
- UML 顺序图(时序图)
- PAG experience: complete AE dynamic deployment and launch all platforms in ten minutes!
猜你喜欢

比尔·盖茨晒48年前简历:“没你们的好看”

UML sequence diagram (sequence diagram)

【立体匹配论文阅读】【三】INTS

leetcode:648. 单词替换【字典树板子 + 寻找若干前缀中的最短符合前缀】

拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条...

Assign a dynamic value to the background color of DataGrid through ivalueconverter

Ian Goodfellow, the inventor of Gan, officially joined deepmind as research scientist

因员工将密码设为“123456”,AMD 被盗 450Gb 数据?

VSCode 配置使用 PyLint 语法检查器

MRS离线数据分析:通过Flink作业处理OBS数据
随机推荐
GVIM [III] [u vimrc configuration]
EfficientNet模型的完整细节
Base64 encoding
Applet directory structure
比尔·盖茨晒48年前简历:“没你们的好看”
c#利用 TCP 协议建立连接
Horizontal of libsgm_ path_ Interpretation of aggregation program
2022pagc Golden Sail award | rongyun won the "outstanding product technology service provider of the year"
Csma/cd carrier monitoring multipoint access / collision detection protocol
Vscode configuration uses pylint syntax checker
常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼
《微信小程序-进阶篇》组件封装-Icon组件的实现(一)
The longest ascending subsequence model acwing 1012 Sister cities
Substance Painter笔记:多显示器且多分辨率显示器时的设置
半小时『直播连麦搭建』动手实战,大学生技术岗位简历加分项get!
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条...
杭电oj2054 A == B ? ???
用例图
设备故障预测机床故障提前预警机械设备振动监测机床故障预警CNC震动无线监控设备异常提前预警
GAN发明者Ian Goodfellow正式加入DeepMind,任Research Scientist