This article describes the use of PHP to jump out of the loop break, and skip a layer of cyclic continue specific usage, with the need for a friend reference. This article leads you to understand the use of break and continue, and their role in looping statements. 1, break(int); the function of statement//break is to exit the loop body, and when the break is followed by a number, the loop that exits the first layer. Used in the while, Do...while, for, foreach, switch statements. Example:
If you add a number to the break, the execution of the loop statement changes. Example:
Code Description: $i =1 $j =1 the loop is over. When the first loop is performed, when the $i=1 begins to execute the first layer while the Loop body code, along the output $i=1, enters the second layer while loop, after the If judgment, the $j is not equal to 2, does not execute the IF body of break 2; This block of statements instead executes the OUTPUT statement output $j= 1 then the second cycle of the second judgment, then $j=2 executed the if body in the break 2; Note: The break behind here is the number 2 indicates that the two-layer loop body code is exited, that is, the first and second layers of the loop body are out of the judgment loop, which results in the above output: $i =1 $j = 1. 2,continue (int); statement //continue statement and break statement execution results are similar, continue is skipping a few layers of loops, ignoring the code snippet in the jumping loop body, executing directly to the end of the loop body for the next loop, As with the break statement, it can be used in the while, Do...while, for, foreach, switch statements. Example:
Code Description: If you do not add an if judgment statement in the while loop body, the result of the output is: $i =1 $i =2 $i =3 $i = 4; But the If judgment statement is added, the output is: $i =1 $i =3 $i = 4, which is the effect of the continue statement. Continue statement is skipped when the $j++ after 2, that is, $j=2 when the loop will be executed continue skip while loop body statement, directly back to the end of the loop body (that is, jump to the head of the loop body) to continue the next loop operation, When you add a number to the continue statement, the result of the output has a new change. Example:
Code Description: Output: $b =1 $b =3 $a =2 perform the outer loop first, $a = 1, the condition is "true", the second layer loop $b=1, the condition is "true", after the If judgment, for "false" do not execute continue statement, execute echo ' $b: '. $b; this statement, The output is $b:1, and then the inner layer while loop, here $b self-increment $b=2, the inner layer while condition continues to "true", continue to perform the inner loop, enter if judgment, the result if condition is judged as "true" execution "continue 2;" This statement directly skip the two-layer loop body code to the end of the parent loop (that is, the head), the parent, sub-layer of the Loop body code is not executed, and then continue to judge the parent cycle condition $a=2 less than 3 set up, again into the inner loop, then $b=3 inner loop condition is established (true), if the condition is false Do not perform "continue 2;", execute "Echo ' $b: '. $b;" statement, output $b=3, and then continue to judge the inner while loop condition, here $b=4, the loop condition is not established, the end of the inner while loop, execute "$b =0;echo ' $a: '. $a; Echo ';" These three statements later to the outer while loop, again to determine the condition, where $ A is already equal to 3, the condition is not set, the outer loop is also over. Note: The difference between break and continue can be followed by a specific number, but break is when the condition is established and exits the number of loops that follow the numbers behind it (if no number is exiting the loop body), and Continue is to jump out of the condition of the number of the body behind it to decide on a few layers of the loop (if there is no number is to exit the loop body), the loop body in the code snippet is not executed, go to the tail of the loop body (that is, the head) and then continue the cycle condition judgment; In summary, break is to exit the loop body execution, and continue is to skip the execution of a layer of the loop body that conforms to the judgment condition statement, and go to a layer loop to continue executing the loop body until the loop condition is not established to end the loop statement. 3,exit () and Die () statement Die is the alias of exit, they do the same thing, is the end of the entire script execution, often used in such as the data into the database, to determine whether a file exists and other occasions. |