Some uncommon for loop statements in PHP

Source: Internet
Author: User

The for statement is the most basic statement in the loop control part of PHP (also multiple languages). The execution rules and basic usage of the for statement are not described here, see the for statement section in the PHP manual. The syntax of PHP is defined as follows:

For Loop statementIt is the most complex loop statement in PHP. Its syntax is as follows:

For (expr1; expr2; expr3) statement

Example

The Code is as follows: Copy code

<Html> <body> <? Phpfor ($ I = 1; $ I <= 5; $ I ++) {echo $ I ;}?> </Body>

The result returned by this instance is:

12345


The following describes several useful variants of the for statement.

1. Infinite Loop

The first is an infinite loop (also known as an "endless loop "). The null expression is effective in syntax, so we can leave the three expressions of the for statement empty, which will produce continuous execution of the for nested statement.

The Code is as follows: Copy code

<? Php
For (;;){
// Place the statements that require continuous execution
}
?>

Although some tasks use infinite loops, most program tasks, especially PHP fields, add conditions for terminating loops when using infinite loops.

The Code is as follows: Copy code

<? Php
For (;;){
// If it is 2199 AD, it will jump out of the loop
If (date ('y') = '000000 '){
Break;
}
}
?>

2. Use an empty expression

Next we will talk about the use of the null syntax in the initialization statement expr1. The most common function of leaving expr1 blank is to complete more complex initialization.

The Code is as follows: Copy code

<? Php
If (isset ($ I )){
Unset ($ I );
If (int) date ('') <2008 ){
$ I = 0;
} Else {
$ I = 1;
}
} Else {
$ I = 3;
}

For (; $ I <10; $ I ++ ){
Echo $ I;
}
?>

In the same way, the iteration expression expr3 may also be left blank. You can also use this to write more complex iterations, such as calling different iterations based on different conditions.

The expr2 field in the for statement is an infinite loop mentioned above. Of course, you can add more complex conditions to determine when to jump out of the loop, which is not repeated here.
3. Multiple cycles

Using multiple loops to control multiple variables is also a feature that is often ignored in the for statement. In the following example, a double loop is usually used in a general task, and a triple or above loop is of little significance.

The Code is as follows: Copy code

<? Php
For ($ I = 0, $ j = 10; $ I <= 10; $ I ++, $ j --){
Echo "$ I + $ j = 10rn ";
}
?>

The above code will be output:

0 + 10 = 10
1 + 9 = 10
2 + 8 = 10
3 + 7 = 10
4 + 6 = 10
5 + 5 = 10
6 + 4 = 10
7 + 3 = 10
8 + 2 = 10
9 + 1 = 10
10 + 0 = 10

4. More complex expressions

If the three expressions of the for statement are more complex, they can be used to optimize the algorithm. You can even use a for statement without a loop body to complete some tasks. For example, calculation of accumulation or factorial:

The Code is as follows: Copy code

<? Php
// Calculates the accumulate result from 1-5, and returns the value to $ j.
For ($ I = 1, $ j = 0; $ I <= 5; $ j + = $ I ++ );
Echo $ j;

// Calculate the factorial result from 1-5. The bin value is $ j.
For ($ I = 1, $ j = 1; $ I <= 5; $ j * = $ I ++ );
Echo $ j;

?>

Use the for loop to return data for the last three months. We all know that using str_pad (intval ($ month), 2, 0, STR_PAD_LEFT) can easily get the last month's time.

The Code is as follows: Copy code
<? Php
$ Y = date ('y'); // get the current time year
$ M = date ('M'); // obtain the current time month
If ($ m = 1) $ time = ($ Y-1 ). '-12'; // if it is July January, the last month is July December, so the year here is equal to 1.
Else $ time = $ y. "-". str_pad (intval ($ m-1), STR_PAD_LEFT); // obtain the last month's time
?>

If you need data from the first three months, it is clear that this processing method is not enough, and similar code is repeatedly executed, which is inefficient. In this case, select the for loop.

The Code is as follows: Copy code
<? Php
// For Loop Variable description $ sm is the starting month $ em is the ending month $ ny is the actual year
If ($ m = 1)
{
$ Sm = 11; // the current month is January. If the previous three months are calculated, the start month is last November.
$ Em = 13; // here is the end mark of the for loop. The end month is January 1, January this year, and 12 is added to facilitate the loop.
}
Elseif ($ m = 2)
{
$ Sm = 12; // same as above
$ Em = 14;
}
Else
{
$ Sm = $ m-3;
$ Em = $ m;
}
For ($ x = $ sm; $ x <$ em; $ x ++)
{
If ($ m = 1 | $ m = 2)
{
If ($ x> = 11) $ ny = $ y-1; // The actual year is last year.
If ($ x> = 13) $ x = $ x-12; // The actual month is this year.
}
Else $ ny = $ y;
// The year and month have all been successful, so it will be easy to do later ......
}
?>

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.