Explanation of "for" loop statement examples of PHP loop control statements

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

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.