PHP flow control for loop control statement
Student Wang traveled back and forth between Beijing and Dalian repeatedly, and recorded the number of round trips in his notebook. There is another implementation in PHP that can achieve the same counting.
The for loop is a counting loop in PHP, and its syntax is quite variable. This is a knowledge point that must be mastered.
for (表达示1; 表达示2; 表达示3){ 需要执行的代码段 }
- Expression 1 is an initialization assignment, and multiple codes can be assigned at the same time.
- Expression 2 is evaluated before each loop starts. If the value is TRUE, the loop continues and the nested loop statement is executed. If the value is FALSE, the loop is terminated.
- Expression 3 is evaluated after each loop.
Let’s write a simple example and see:
'; } ?>
In other words, let’s try to judge multiple conditions:
'; } ?>
We use for Let’s type the 9*9 multiplication table in a loop. The demonstration effect is as follows:
Remember during the analysis and thinking process: the code is output horizontally
'; }
Let's try break, exit and continue to control the 9*9 multiplication table.
Statement | Function |
---|---|
Before exit We have talked about stopping subsequent execution from the current location | |
I have encountered it before, jumping out of the loop or structure to execute subsequent code | |
Jump out of this loop and continue the next loop |
'; } ?>$i is equal to 4, the effect of break is as follows:
Note: No longer executed after the 4th point in the above figure
Note : The 4th one in the picture above is lost, and then the
job is continued from the 5th year: Use a single-layer for loop to control the table that changes color on alternate rows
Use the double-layer loop of for to control the color-changing table of alternate rows
Write 99 multiplication tables silently, and experiment with continue and break at the positions of $i and $j in the middle;