Several "perverted" usages of a For loop statement in PHP

Source: Internet
Author: User
Tags add date manual empty execution
Loop | Statement For statement can be said to be PHP (but also many languages) of the loop control part of the most basic statement of the execution of the For statement and the basic usage here is not much to say, you can see the PHP Manual for statement section. 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.

<?php
for (;;) {
 //放置需要不断执行的语句
}
?>

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.

<?php
for (;;) {
 //如果是公元2199年,则跳出循环
 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.

<?php
if (isset($i)) {
 unset($i);
 if ((int) date('') < 2008) {
  $i = 0;
 } else {
  $i = 1;
 }
} else {
 $i =3;
}

for (;$i < 10;$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.

<?php
for ($i = 0, $j = 10;$i <= 10;$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:

<?php
//计算1-5的累加结果,斌值到$j
for ($i = 1,$j = 0; $i <= 5;$j += $i++);
echo $j;

//计算1-5的阶乘结果,斌值到$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.