Home  >  Article  >  Backend Development  >  How to use break keyword in php?

How to use break keyword in php?

黄舟
黄舟Original
2017-06-25 09:36:311632browse

I know that break in PHP can break out of the loop, so what does it mean if you add numbers 2 or 3 after break? For example: break 2; What does this mean? What is the difference between break ; ? ? ? Can you give an example? Thank you thank you thank you thank you! ! ! ! ...

It means how many levels can be jumped out of in a nested loop or multiple switch structure. The default is break 1, 1 can be omitted.
For example:

foreach($arr as $v){
    foreach($v as $_v){
        if($_v=='a') break 2;
    }
}

This is a double foreach traversal. When the element value is a, it jumps out of the 2-level traversal. That is to say, even if there is still data that has not been traversed, the foreach traversal will no longer be performed. .

Note that if is not included in the hierarchy, only break can be used in for/foreach/while/do-while/switch to jump out.

$a=10;
while($a){
    switch($a){
        case 7: echo 'haha';break;
        case 6:echo 'yoyo';break;
        default:
            if($a!=3){   
               echo 'nini';
            }else{
                 break2;
            }
            break;
    }
    $a--;
}

Let me explain the statement. First, a variable $a is defined and assigned a value of 10. Then while loop is performed. The condition of the loop is when $a is a valid value. The valid value here means that the value of $a is not false (that is, $a cannot be 0), because at the end of while There is $a--, the value of variable $a will decrease, and it will always decrease to 0 in a loop.
There is a switch statement in the while loop to judge the current value of $a. If it is 7, it will print haha. If it is 6, it will print yoyo. After printing, the switch will jump out. The default is default. Determine if the value of $a is not 3, then print a nini, and then end the switch. $a will be decremented by 1 and then proceed to the next loop. If $a is equal to 3, then break 2 will jump out of the double loop. At this time The while loop will also be ended, regardless of whether $a has not decremented to 0.

I would like to ask continue Can this keyword that jumps out of the loop be increased the number of times? For example: continue 2; , skip two loops. I tried it and found that it will report an error
Thank you, thank you, thank you, thank you! ! ! ! ! ! Thank you thank you thank you thank you! ! ! ! ! ! ! thanks, thanks! ! ! ! !

Can be added, continue must also be used with if and other conditional statements. Different from break, break will terminate the entire loop, while continue will end this loop and proceed to the next loop.

break; 跳出当前循环或Switch
break N; 跳出N层循环。
switch $case{
case "1":

breack;
case "2":
bereak 2;

}

The above is the detailed content of How to use break keyword in php?. 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