Introduction to the usage of cyclic statements in several useful variant PHP _php tips

Source: Internet
Author: User
Its syntax is defined in the PHP manual as follows:
for (EXPR1; expr2; expr3)
Statement
Here are some useful variants of the for statement.
1, Infinite cycle
The first is the infinite circle known to all (also known as the "Dead Loop"). Because null expressions are syntactically valid, we can leave three expressions of the For statement blank, which results in the execution of a for nested statement.
for (;;) {
Place statements that need to be executed continuously
}
?>
While there are some tasks that use infinite loops, most program tasks, especially the areas that PHP can cover, add conditions to terminate loops when using an infinite loop.
for (;;) {
If it is 2199, then jump out of the loop
if (date (' Y ') = = ' 2199 ') {
Break
}
}
?>
2, the use of empty expressions
The next thing to say is that null syntax is used in the initialization statement expr1, and the most common function of expr1 is to do more complex initialization work.
Copy 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;
}
?>

By the same token, the EXPR3 may also be left blank, and you can use this 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 infinite loop mentioned above, and of course you can add some more complex conditions to determine when to jump out of the loop, not repeat here.
3. Multi-cycle
Using multiple loops to control multiple variables is also an attribute that is often ignored in a for statement. As the following example, in the general task used in the general will be a double loop, triple above the cycle is generally insignificant.
  
Copy Code code as follows:

<?php
for ($i = 0, $j = $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 expressions 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 the cumulative or factorial:
Copy Code code as follows:
  
<?php
Calculates the cumulative result of 1-5, the 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 syntax of C language to some extent also has the characteristics of C, such as a powerful for loop statement is a typical example.

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.