只要指定条件为真,则循环代码块
while (条件为真) {
要执行的代码;
}
先执行一次代码块,然后指定条件为真则重复循环
do {
要执行的代码;
} while (条件为真);
while与do...while的差别在判断条件不成立时(初次),do...while还会执行语句。
<?php
header("content-type:text/html;charset=utf-8");
$a = 19;
while ($a<6)
{
echo "数字是:$a <br/>";
echo "while";
$a++;
}
do
{
echo "数字是:$a <br/>";
echo "do...while";
$a++;
} while ($a<6)
?>
循环代码块指定次数
for (init counter; test counter; increment counter) {
code to be executed;
}
<?php
header("content-type:text/html;charset=utf-8");
for ($a=0; $a<10; $a++)
{
echo "数字是:$a<br/>";
}
?>
只适用于数组,遍历数组中的每个元素并循环代码块
foreach ($array as $value)
{
code to be executed;
}
<?php
$colors =array("red","green","yellow","blue");
foreach ($colors as $tmp)
{
echo "$tmp <br/>";
}
?>
原文:https://www.cnblogs.com/ceiling-/p/14277530.html