A for loop is a complex loop structure in PHP. It has three expressions. in this chapter, we will explain the "for" loop in the PHP loop control statement.
A for loop is a complex loop structure in PHP. It has three expressions.Syntax formatAs follows:
for (expr1; expr2; expr3){ statement;}
Syntax explanation:
The first expression expr1 is executed only once at the beginning of the loop.
The second expression expr2 is executed at the beginning of each loop of the loop body. if the execution result is true, the statement is executed. Otherwise, the loop jumps out and runs down.
The third expression expr3 is executed after each loop.
For loop statement flow control diagram
We can regard the for loop as a compact and simplified while loop, as shown below,
Code written using the while loop:
You can use the for loop as follows:
The two types of code run the same result. Therefore, the for and while loops can be considered as equivalent functions.
For loop instance
This instance uses the for loop and outputs a number of less than 5
";}?>
Code running result:
The above is a simple application of the for loop. remember that when using the loop, make sure that the loop can end and there is no endless loop, when we talk about the "while" loop statement, we have already introduced it. if you are not familiar with it, let's take a look. I will not introduce it too much here. in the next section, we will introduce a special loop statement "foreach loop" in PHP ".
The above is a detailed description of the "for" loop statement instance of PHP loop control statements. for more information, see other related articles in the first PHP community!