Using break keyword: The break keyword is used to immediately terminate the loop and the program control resumes at the next statement following the loop. To terminate the control from any loop we need to use break keyword. The break keyword is used to end the execution of current for, foreach, while, do-while or switch structure. But in nested loops, to exit out from all or some of the outer loops, we need to pass a numeric argument which tells it how many nested enclosing structures are to be terminated.
Parameters: The break keyword followed by a numeric argument which is by default 1. Passing of variables and 0 as a numeric argument is not allowed.
break 2; // It terminates second outer loop break 3; // It terminates third outer loop break $num; // Variable cannot be used as numeric argument since version 5.4.0 break 0; // 0 is not a valid argument to pass
Below programs illustrate how to break outer loops in PHP:
Program 1: PHP program to display the number while inner loop is not terminated.
1 2 3
Program 2: PHP program to search a number in an array and break the outer loop when it is found.
45 is found in the array
Using goto keyword: The goto keyword is used to jump the section of the program. It jumps to the target label.
Program 3: PHP program to break the outer loop using goto keyword.
45 is found in the array
- What is the difference between “word-break: break-all” versus “word-wrap: break-word” in CSS ?
- Switch Case with Break in For Loop in Golang
- Difference between Left, Right and Full Outer Join
- Difference between Inner Join and Outer Join in SQL
- How to get all HTML content from DOMParser excluding the outer body tag ?
- How to get the outer html of an element using jQuery ?
- PHP break (Single and Nested Loops)
- Difference between break and continue in PHP
- What is the use of number after “break” or “continue” statements in PHP ?
- CSS page-break-before Property
- CSS | box decoration break Property
- CSS | word-break Property
- How to break line without using <br> tag in HTML / CSS ?
- Tips for restarting career after a break
- break command in Linux with examples
- CSS | page-break-after Property
- CSS | page-break-inside Property
- How to insert line break before an element using CSS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mitrajit