PHP for loop Writing Method and example, phpfor writing example
A For Loop is one of the most recent loop statements. In any language, this loop statement is also a commonly used loop method in our work.
Syntax Rules:
For (expr1; expr2; expr3 ){
Code to be executed
}
Expr1: the place where the loop starts
Expr2: the condition of the loop. If the value is TRUE, the loop continues and the nested loop statement is executed. If the value is FALSE, the loop is terminated.
Expr3: is evaluated (and executed) after each loop ).
It's a bit obscure. Let's write the simplest for loop demo!
For Loop demo1:
<? Phpfor ($ n = 1; $ n <20; $ n ++) {echo 'for Loop statement execution No. $ n. "Times <br> ";}
Execution result:
For Loop statement execution 1st times
For Loop statement execution 2nd times
For Loop statement execution 3rd Times
For Loop statement execution 4th times
For Loop statement execution 5th times
For Loop statement execution 6th times
For Loop statement execution 7th times
For Loop statement execution 8th times
For Loop statement execution 9th times
For Loop statement execution 10th times
For Loop statement execution 11th times
For Loop statement execution 12th times
For Loop statement execution 13th times
For Loop statement execution 14th times
For Loop statement execution 15th times
For Loop statement execution 16th times
For Loop statement execution 17th times
For Loop statement execution 18th times
For Loop statement execution 19th times
It can be seen that $ n is not output when the $ n <20 condition is not met.
For Loop statement Demo2, use break to exit the for Loop:
<? Phpfor ($ n = 1; $ n <20; $ n ++) {if ($ n = 10) {break;} echo 'for Loop statement execution '. $ n. "Times <br> ";}
Output result:
For Loop statement execution 1st times
For Loop statement execution 2nd times
For Loop statement execution 3rd Times
For Loop statement execution 4th times
For Loop statement execution 5th times
For Loop statement execution 6th times
For Loop statement execution 7th times
For Loop statement execution 8th times
For Loop statement execution 9th times
When n is equal to 10, it jumps out of the loop and does not continue execution. If we only want to jump out of 10 and execute other operations, we can write as follows:
<? Phpfor ($ n = 1; $ n <20; $ n ++) {if ($ n = 10) {continue;} echo 'for Loop statement execution '. $ n. "Times <br> ";}
In this way, we just jump out of a loop and the result is as follows:
For Loop statement execution 1st times
For Loop statement execution 2nd times
For Loop statement execution 3rd Times
For Loop statement execution 4th times
For Loop statement execution 5th times
For Loop statement execution 6th times
For Loop statement execution 7th times
For Loop statement execution 8th times
For Loop statement execution 9th times
For Loop statement execution 11th times
For Loop statement execution 12th times
For Loop statement execution 13th times
For Loop statement execution 14th times
For Loop statement execution 15th times
For Loop statement execution 16th times
For Loop statement execution 17th times
For Loop statement execution 18th times
For Loop statement execution 19th times
My blog: PHP for loop Writing Method and example