- PHP also has two keywords break and continue to control the loop, just like another programming language.
- Those statements are known as the flow of transfer or jumping statements in the program.
Break

- The break statement is used inside the for loop, foreach loop, while loop and do-while loop and then switch case statement.
- Inside a loop the break statement encountered then it immediately terminated the loop statement and transferred the control to the next statement, followed by a loop to resume the execution.
- In nested loop statement it is also used, where it breaks the inner loop first and then proceed to break the outer loop.
- In program it is also used in the switch case statement to terminate a particular case’s execution.
- A break statement is placed inside the loop with a condition and then if the condition is true, a break statement is immediately executed to break the execution of the loop and to move the control to the next statement followed by the loop.
Sample Code
[pastacode lang=”php” manual=”%3C%3Fphp%C2%A0%C2%A0%C2%A0%C2%A0%0A%0A%2F%2F%C2%A0defines%C2%A0the%C2%A0for%C2%A0loop%C2%A0%C2%A0%C2%A0%0A%0Afor%C2%A0(%24a%C2%A0%3D%C2%A00%3B%C2%A0%24a%C2%A0%3C%C2%A010%3B%C2%A0%24a%2B%2B)%C2%A0%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0if%C2%A0(%24a%C2%A0%3D%3D%C2%A07)%C2%A0%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0break%3B%C2%A0%2F*%C2%A0Break%C2%A0the%C2%A0loop%C2%A0when%C2%A0condition%C2%A0is%C2%A0true.%C2%A0*%2F%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%7D%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0echo%C2%A0%22Number%3A%C2%A0%24a%C2%A0%3Cbr%3E%22%3B%C2%A0%C2%A0%0A%0A%7D%C2%A0%C2%A0%0A%0Aecho%C2%A0%22%C2%A0Terminate%C2%A0the%C2%A0loop%C2%A0at%C2%A0%24a%C2%A0number%22%3B%C2%A0%C2%A0%0A%0A%3F%3E%C2%A0%C2%A0″ message=”” highlight=”” provider=”manual”/]Output

Continue

- It is used in the middle of for loop, while loop, do-while loop and for-each loop.
- In a loop the continue statement is encountered then it skips the current iteration of the loop.
- A continue statement is usually used with a condition inside a loop and then if the condition is true, the continue statement is executed to skip the iteration.
- It transfers the control to the beginning of the loop for further execution inside the loop, after that.
Sample Code
[pastacode lang=”php” manual=”%3C%3Fphp%C2%A0%C2%A0%C2%A0%C2%A0%0A%0A%2F%2F%C2%A0defines%C2%A0the%C2%A0for%C2%A0loop%C2%A0%C2%A0%C2%A0%0A%0Afor%C2%A0(%24a%C2%A0%3D%C2%A00%3B%C2%A0%24a%C2%A0%3C%C2%A07%3B%C2%A0%24a%2B%2B)%C2%A0%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0if%C2%A0(%24a%C2%A0%3D%3D%C2%A05)%C2%A0%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0echo%C2%A0%22%C2%A0Skipped%C2%A0number%C2%A0is%C2%A0%24a%C2%A0%3Cbr%3E%22%3B%C2%A0%2F%2F%C2%A0prints%C2%A0the%C2%A0skipped%C2%A0number.%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0continue%3B%C2%A0%2F*%C2%A0It%C2%A0skips%C2%A0the%C2%A0defined%C2%A0statement%C2%A0if%C2%A0the%C2%A0condition%C2%A0is%C2%A0true.%C2%A0*%2F%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%7D%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0echo%C2%A0%22Number%C2%A0is%3A%C2%A0%24a%C2%A0%3Cbr%3E%22%3B%C2%A0%C2%A0%0A%0A%7D%C2%A0%C2%A0%0A%0A%3F%3E” message=”” highlight=”” provider=”manual”/]Output

