php学习之旅 (5) 循环

php学习之旅 (5) 循环
1、语法

while - 循环直到特定条件满足
do...while - 执行一次代码,并且循环,直到满足特定条件
for - 循环指定的次数
foreach - 对数组中的每个元素都循环一遍

2、while
<?php
$i=1;
while($i<=5)
   {
   echo "The number is " . $i . "<br />";
   $i++;
   }
?>

输出
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

3、do...while
<?php
$i=1;
do
   {
   $i++;
   echo "The number is " . $i . "<br />";
   }
while ($i<=5);
?>


输出
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6

4、for
<?php
for ($i=1; $i<=5; $i++)
{
   echo "The number is " . $i . "<br />";
}
?> 


输出

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

5、foreach

<?php
$x=array("one","two","three");
foreach ($x as $value)
{
   echo $value . "<br />";
}
?>

输出
one

分享我的文章 程序员赚钱之路探索


你可能感兴趣的:(PHP,Blog)