$ab = 0;
$xy = 1;
echo "<table>";
$i = 0;
while ($i < 5) {
echo "<tr><td>$ab</td><td>$xy</td></tr>";
$ab += $xy;
$xy += $ab;
$i++;
}
echo "</table>";
对于解释:
相较于 “为”循环,你必须在打开循环之前初始化“计数器”[$ i = 0]
在循环中,您所指定的条件继续循环[$ I < 5]
而且某处进入死循环,你增加你的“反击” [$ I ++]
你的“计数器”,可以增加或减少,或直接设置;这完全是关于你的代码逻辑和你的需求。
while ($i < 5) {
echo "<tr><td>$ab</td><td>$xy</td></tr>";
$ab += $xy;
$xy += $ab;
if ($ab == 22) { // If $ab is equal to a specific value
/* Do more stuff here if you want to */
break; // stop the loop here
}
$i++;
}
这个例子也与“for”循环工作:
你也可以随时在你所需要,看一个例子情况下,要打破循环。
而且还有另一个关键字,“继续”,用来告诉“跳”到下一个循环迭代:
while ($i < 5) {
$i++; // Don't forget to increase "counter" first, to avoid infinite loop
if ($ab == 22) { // If $ab is equal to a specific value
/* Do more stuff here if you want to */
continue; // ignore this iteration
}
/* The following will be ignored if $ab is equal to 22 */
echo "<tr><td>$ab</td><td>$xy</td></tr>";
$ab += $xy;
$xy += $ab;
}
那么你有甚至尝试一些东西,得到了地方停留,还是你只是要求我们的代码为你? – Rizier123
'while($ i <5)'...然后在'while'中增加'$ i',否? http://php.net/manual/en/control-structures.while.php – chris85
确实这是一个很好的策略,实际上_learn_东西?通过询问问题的答案? – arkascha