Home  >  Article  >  Backend Development  >  Some uncommon methods of for loop statements in PHP_PHP Tutorial

Some uncommon methods of for loop statements in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:16:17989browse

The for statement can be said to be the most basic statement in the loop control part of PHP (and in many languages). The execution rules and basic usage of the for statement will not be discussed here. You can refer to the for statement section of the PHP manual. . Its syntax is defined in the PHP manual as follows:

for loop statement is the most complex loop statement in the PHP language. Its syntax is as follows:

for (expr1; expr2; expr3)statement

Example

The code is as follows Copy code
 代码如下 复制代码

该实例返回的显示结果是:

12345


The display result returned by this instance is:

12345

Here are some useful variations of the for statement.
 代码如下 复制代码

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

1. Infinite loop

 代码如下 复制代码

for (;;) {
 //如果是公元2199年,则跳出循环
 if (date('Y') == '2199') {
  break;
 }
}
?>

First of all, there is the infinite loop (also known as "dead loop") that everyone knows. Since the empty expression null is syntactically valid, we can leave the three expressions of the for statement empty, which will have the effect of continuously executing nested for statements.

The code is as follows Copy code
for (;;) {

//Place statements that need to be executed continuously
代码如下 复制代码

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

for (;$i < 10;$i++) {
 echo $i;
}
?>

} ?> Although some tasks will use infinite loops, most program tasks, especially those involving PHP, will add some conditions to terminate the loop when using infinite loops.
The code is as follows Copy code
for (;;) {<🎜> //If it is 2199 AD, break out of the loop <🎜> if (date('Y') == '2199') {<🎜> break;<🎜> }<🎜> }<🎜> ?>
2. Use empty expressions The next step is to talk about using null syntax in the initialization statement expr1. The most common function of leaving expr1 blank is to complete more complex initialization work.
The code is as follows Copy code
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. This can also be used to write more complex iterations, such as calling different iterations according to different conditions.

Leaving the conditional statement expr2 in the for statement blank is the infinite loop mentioned above. Of course, you can also add some more complex conditions to determine when to jump out of the loop, which will not be repeated here.
3. Multiple loops

Using multiple loops to control multiple variables is also a feature that is often overlooked in the for statement. As in the example below, double loops are generally used in general tasks, and loops of three or more are generally of little significance.

The code is as follows
 代码如下 复制代码

for ($i = 0, $j = 10;$i <= 10;$i++, $j--) {
 echo "$i + $j = 10rn";
}
?>

以上代码将输出:

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

Copy code

for ($i = 0, $j = 10;$i <= 10;$i++, $j--) {

代码如下 复制代码

//计算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;

?>

echo "$i + $j = 10rn";

}

?>
 代码如下 复制代码
$y = date('Y');//获取当前时间年份
$m = date('m');//获取当前时间月份
if($m == 1) $time = ($y-1).'-12';//如果是1月份,则上个月就是去年的12月,所以这里的年份要减去1
else $time = $y."-".str_pad(intval($m-1),2,0,STR_PAD_LEFT);//获取上个月时间
?>

The above code will output:

 代码如下 复制代码
//for循环变量说明 $sm为起始月份 $em为结束月份 $ny为实际年份
if($m == 1)
{
  $sm = 11;//当前月份是1月,以前三个月计算的话,起始月份就是去年11月
  $em = 13;//这里做为for循环结束标记,结束月份是今年1月,加上12只是便于循环
}
elseif($m == 2)
{
  $sm = 12;//同上
  $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;//实际年份是去年
    if($x >= 13) $x = $x - 12;//实际月份是今年
  }
  else $ny = $y;
  //年份、月份都顺利得到了,后面就好办了……
}
?>
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 written more complexly, 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, calculating accumulation or factorial:
The code is as follows Copy code
//Calculate the cumulative result of 1-5, bin value to $j<🎜> for ($i = 1,$j = 0; $i <= 5;$j += $i++);<🎜> echo $j;<🎜> <🎜>//Calculate the factorial result of 1-5, bin value to $j<🎜> for ($i = 1,$j = 1; $i <= 5;$j *= $i++);<🎜> echo $j;<🎜> <🎜>?> Use a for loop to return the data of the past three months. We all know that str_pad(intval($month),2,0,STR_PAD_LEFT) can easily obtain the time of the previous month.
The code is as follows Copy code
$y = date('Y');//Get the current time and year<🎜> $m = date('m');//Get the current time and month<🎜> if($m == 1) $time = ($y-1).'-12';//If it is January, then the last month is December last year, so the year here should be subtracted by 1<🎜 > else $time = $y."-".str_pad(intval($m-1),2,0,STR_PAD_LEFT);//Get the time of last month<🎜> ?> If you need the data of the first three months, obviously this processing method is not enough, and similar codes are executed repeatedly, which is not efficient. At this time, choose a for loop.
The code is as follows Copy code
//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 calculated in the previous three months, the starting month is November last year<🎜> $em = 13;//This is used as the end mark of the for loop. The end month is January this year. Adding 12 is just 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; //I got the year and month successfully, it will be easy to handle later... } ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628705.htmlTechArticleThe for statement can be said to be the most basic statement in the loop control part of PHP (and many languages) , the execution rules and basic usage of the for statement will not be discussed here. You can refer to P...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn