while
只要指定条件为真,则循环代码块
语法
while (条件为真) {
要执行的代码;
}
do...while
先执行一次代码块,然后指定条件为真则重复循环
语法
do {
要执行的代码;
} while (条件为真);
while与do...while的差别在判断条件不成立时(初次),do...while还会执行语句。
header("content-type:text/html;charset=utf-8");
$a = 19;
while ($a<6)
{
echo "数字是:$a
";
echo "while";
$a++;
}
do
{
echo "数字是:$a
";
echo "do...while";
$a++;
} while ($a<6)
?>
for
循环代码块指定次数
语法
for (init counter; test counter; increment counter) {
code to be executed;
}
参数
init counter:初始化循环计数器的值 (初始值)
test counter:评估每个循环迭代。如果值为 TRUE,继续循环。如果它的值为 FALSE,循环结束。(判断条件)
increment counter:增加循环计数器的值
举个例子
header("content-type:text/html;charset=utf-8");
for ($a=0; $a<10; $a++)
{
echo "数字是:$a
";
}
?>
foreach
只适用于数组,遍历数组中的每个元素并循环代码块
语法
foreach ($array as $value)
{
code to be executed;
}
参数
$array:数组的变量名
$value:接受数组值的变量名
$colors =array("red","green","yellow","blue");
foreach ($colors as $tmp)
{
echo "$tmp
";
}
?>
标签:语句,do,...,代码,counter,while,循环,PHP
来源: https://www.cnblogs.com/ceiling-/p/14277530.html