Dream West Tour 3 mountains and rivers map of what to use several useful variants introduction to the use of cyclic statements in PHP

Source: Internet
Author: User
The syntax for this PHP manual is defined as follows:
for (EXPR1; expr2; expr3)
Statement
Here are some useful variants of the for statement.
1. Infinite loop
The first is a well-known infinite loop (also known as a "dead loop"). Because null expressions are syntactically valid, we can leave the three expression of the For statement blank, which results in a continuous execution of the for nested statement.
for (;;) {
Placing statements that need to be executed continuously
}
?>
While there are some tasks that can be used in an infinite loop, most program tasks, especially the areas that PHP can relate to, add some conditions for terminating loops when using an infinite loop.
for (;;) {
If it is ad 2199, jump out of the loop
if (date (' Y ') = = ' 2199 ') {
Break
}
}
?>
2. Using an empty expression
The next thing to say is to use the null syntax in the initialization statement expr1, and the most common function of leaving the empty expr1 is to do more complex initialization work.

Copy the Code code as follows:


if (Isset ($i)) {
Unset ($i);
if ((int) date (') < 2008) {
$i = 0;
} else {
$i = 1;
}
} else {
$i = 3;
}
for (; $i <; $i) {
echo $i;
}
?>


In the same vein, the EXPR3 may also be left blank, and can be used to write more complex iterations, such as invoking different iterations based on different conditions.
The conditional statement in the For statement expr2 empty is the above-mentioned infinite loop, of course, you can add some more complex conditions to determine when to jump out of the loop, there is no repetition.
3. Multiple loops
Using multiple loops to control multiple variables is also an attribute that is often ignored in a for statement. As the following example, in general tasks used in general will be a double cycle, more than a triple of the general meaning of the cycle.

Copy the Code code as follows:


for ($i = 0, $j = ten; $i <=; $i, $j-) {
echo "$i $j = 10\r\n";
}
?>


The above code will 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 you write the three expression of a for statement more complex, you can use it to optimize the algorithm. You can even use a for statement without a loop body to accomplish some tasks. For example, calculate summation or factorial:

Copy the Code code as follows:


Calculates the cumulative result of 1-5, bin value to $j
for ($i = 1, $j = 0; $i <= 5; $j = $i);
Echo $j;
Calculates the factorial result of 1-5, the bin value to $j
for ($i = 1, $j = 1; $i <= 5; $j *= $i);
Echo $j;
?>


PHP uses the C language syntax to some extent also has C features, such as a powerful for loop statement is a typical example.

The above describes the Dream West Tour 3 mountains and rivers of the map has what to use several useful variants of the PHP Circular statement usage introduction, including the Dream of the West Tour 3 mountains and rivers map What the use of the content, I hope that the PHP tutorial interested friends have some help.

  • 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.