This article mainly introduces how the for loop in PHP is nested, the interested friend's reference, hopes to be helpful to everybody.
How the For loop performs:
The parameters for the For loop are (initial value; judging condition; updating loop variable expression) all three are not necessary, if the three are incomplete then you must manually invoke the break command to terminate the loop at the appropriate time, otherwise the loop will continue to become a dead loop
The execution process is:
First judge whether the initial value satisfies the judging condition, if satisfies then starts the loop, if does not satisfy then skips the loop directly therefore the following loop is not to be executed:
for ($i =0; $i <0; $i + +)
Then execute the loop body (the code wrapped in the curly braces for the back)
Updating a variable with an update loop variable expression
Use judgment conditions to judge, if not satisfied then terminate the loop, if satisfied then execute the loop body again
So the following loop will be executed 5 times
for ($i =0; $i <5; $i + +)
For loop nesting:
For loop nesting, the inner loop is prioritized and the outer loop is executed as follows:
The outer loop begins for ($i =0; $i <10; $i + +) { //Here is the loop body for the outer Loop for ($j =0; $j <20; $j + +)//inside loop start { //Here is the loop body of the inner loop }//inner Loop End}//Outer Loop End
When the loop is started, the loop body of the outer loop (which includes the inner loop) is executed first, in which case the inner loop is executed when the inner loop is performed, the $j is incremented from 0 to 19, the outer loop ends after the loop is completed 20 times, $i + +, and the outer loop is $i=1 again at the time of execution.
In total, the outer loop body is performed 10 times, the inner loop body executes 20 (the repetition of the inner loop itself) *10 (each outer loop performs 20 cycles) = 200 times