PHP BreakPHP break statement breaks the execution of the current for, while, do-while, switch, and for-each loop. If you use break inside inner loop, it breaks the execution of inner loop only. The break keyword immediately ends the execution of the loop or switch structure. It breaks the current flow of the program at the specified condition and program control resumes at the next statements outside the loop. The break statement can be used in all types of loops such as while, do-while, for, foreach loop, and also with switch case. Syntaxjump statement; PHP Break: inside loopLet's see a simple example to break the execution of for loop if value of i is equal to 5. <?php Output: 1 2 3 4 5 PHP Break: inside inner loopThe PHP break statement breaks the execution of inner loop only. <?php Output: 1 1 1 2 1 3 2 1 2 2 3 1 3 2 3 3 PHP Break: inside switch statementThe PHP break statement breaks the flow of switch case also. <?php Output: number is equal to 200 PHP Break: with array of string<?php Output: One Two Three You can see in the above output, after getting the specified condition true, break statement immediately ends the loop and control is came out from the loop. PHP Break: switch statement without breakIt is not essential to break out of all cases of a switch statement. But if you want that only one case to be executed, you have to use break statement. <?php Output: $car is not Mercedes Benz $car is Mercedes Benz PHP Break: using optional argumentThe break accepts an optional numeric argument, which describes how many nested structures it will exit. The default value is 1, which immediately exits from the enclosing structure. <?php Output: At matched condition i = 5 At matched condition i = 10; quitting Note: The break keyword immediately ends the execution of the current structure. |
0 Comments: