How A For loop in PHP can be nested

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.