Home  >  Article  >  Backend Development  >  PHP basic tutorial - PHP loop statement

PHP basic tutorial - PHP loop statement

WBOY
WBOYOriginal
2016-07-25 09:00:121225browse
This section introduces PHP loops to you. PHP loop statements include for, while, do...while, switch, etc. They are probably used the most in actual programming. It is recommended that you have a firm grasp of them.

Welcome to the php loop tutorial page. 1. Break: Indicates the end of the current for, while, do..while, switch process. A number can be given later to indicate which level to retreat to. Such as:

<?php
//循环语句示例
//by bbs.it-home.org
$i = 0;
while(++$i){
switch($i){
case 4:
echo "quit to 4.<br>";
break;//跳出switch语句
case 9:
echo "quto to 9.<br/>";
break 2; //这里跳了2层,跳出while循环 这里的数字不能超过它实际的层数,如果写成break 3,系统就会报错。
default:
break;
}
}
echo 'Over! $i='.$i;
?>

Results: quit to 4. quto to 9. Over! $i=9

2. continue statement: jump out of the remaining code in this loop, and proceed to the next loop when it determines that the condition is true

<?php
//continue语句
//by bbs.it-home.org
for($i=0;$i<13;$i++){
if($i==5){
continue; //跳出本次循环中的剩余代码,并判断本次条件为真的时候进行下一次循环
}
echo '$i='.$i."<br/>";
}
echo "Over";
?>

Results: $i=0 $i=1 $i=2 $i=3 $i=4 $i=6 $i=7 $i=8 $i=9 $i=10 $i=11 $i=12 Over

continue can also be followed by numbers:

<?php
for($i=0;$i<2;$i++){//@
for ($j=1;$j<4;$j++){
if($j==2){
continue 2; //跳出2层循环后执行$i++//@
}
echo '$i='.$i.'$j='.$j.'<br/>';
}
}
echo "Over";
?>

Results: $i=0$j=1 $i=1$j=1 Over

3. goto statement: jumps can only be made in the same file or scope Syntax: goto tag; Label: //..Statement

<?php
goto a;
echo "aa";
a:
echo "bb";
?>

Output: bb

Thank you for paying attention to the PHP introductory tutorials. This series of PHP basic tutorials will help PHP newbies quickly master the PHP programming language. Programmer's Home will continue to launch PHP-related tutorials for everyone, and I wish you all the best in your learning and progress!



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