PHP for loop Writing Method and example, phpfor writing example

Source: Internet
Author: User

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

Related Article

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.