Understanding the Difference Between PHP continue vs break
Summary: In this article, we learn the Difference between php continue vs break in details with example, Let’s understand the difference between them and how to use in PHP?
PHP continue vs break
Just like other programming languages, PHP also has two keywords break and continue to control the loop. These statements are called as jumping statements or flow of transfer in the program. In this article, we will discuss the difference between PHP continue vs break.
It is a statement or keyword which is used to skip the execution of a particular or specific statement inside the loops.
It is a break statement or keyword which is used to terminate and transfer the control to the next statement when encountered inside a loop or switch case statement.
it transfers the control to the beginning or starting of the loop when a continue statement is placed inside a loop.
Control can only be transferred to the next statement followed by a loop if the break encountered within a loop.
Condition-based on if statement.
If the defined condition is true, then the continue statement is executed in the loop or switch.
If the defined condition is true, then the break statement is executed in the loop or switch.
Use in loops and switch-case statements
A continue statement in PHP can also be used inside the switch case. furthermore, the continue statement is also used in various loops such as while, do-while, and foreach loop.
Similarly, a break statement is also used in various loops such as, while, do-while, foreach loop, and the switch case statement.
The continue statement is used in the middle of for loop, do-while loop, while loop, and the foreach loop. because as the continue statement is encountered in a loop, it skips the current iteration of the loop and transfers the control to the starting or beginning of the loop for further execution of the loop. as an example, in some situations where we wish to skip the program’s current statement and then starts the next statement, we tend to use the continue statement.
In the above flow diagram, you can see that a continue statement is used to skip a particular or specific iteration of the loop. It’s usually used with a condition inside a loop. furthermore, if the defined condition is true, then the PHP continue statement is executed to skip the iteration. After that, it transfers the control to the beginning or starting of the loop for further execution inside the loop.
echo" Skipped number is $x <br>";// prints the skipped number.
continue;/* It skips the defined statement if the condition is true. */
echo"Number is: $x <br>";
Number is: 0
Number is: 1
Number is: 2
Number is: 3
Number is: 4
Skipped number is 5
Number is: 6
Number is: 7
Number is: 8
Explanation: As we can see in the above program, for the loop start the execution of the loop from 1 to 9. When the statement of if statement x==5, a continue statement enters the loop and skips the execution of the current statement. After that, transfer the control to the beginning of the loop for further execution of the loop statement.
The break statement is used inside the loop such as for, foreach, while, and do-while, and switch case statement. because the break statement encountered inside a loop, it instantly terminated the loop statement and transferred the control to the next statement, followed by a loop to resume the execution. furthermore, it’s also utilized in the nested loop statement, where it breaks the inner loop first and then proceeds to break the outer loop. It’s also utilized in the switch case statement to terminate a particular or specific case’s execution in the program.
In the above flow diagram, you can see that a break statement is placed inside the loop with a condition. furthermore, if the condition defined in the loop is true, then 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.
Let’s see the program of break statement using for loop in PHP.
// defines the for loop
break;/* Break the loop when condition is true. */
echo"Number: $x <br>";
echo" Terminate the loop at $x number";
Terminate the loop at 9 number
Explanation: As we can see in the above program of break statement using for loop. for loop is continuously executed in the program. When the condition of if statement x==9, the break statement encountered inside the loop and terminates the loop. furthermore, after that, it transfers the control to the next statement followed by for loop to execute the statement.
In this article, you have learned the Difference Between PHP continue vs break with example and flow diagram. I hope you will enjoy it!. if have any query then contact on firstname.lastname@example.org I will try to resolve the problem.
Stay healthy, stay safe! Happy coding! enjoy it!