php学习之旅 (5) 循环
1、语法
while - 循环直到特定条件满足
do...while - 执行一次代码,并且循环,直到满足特定条件
for - 循环指定的次数
foreach - 对数组中的每个元素都循环一遍
2、while
输出
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
3、do...while
输出
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
4、for
输出
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
5、foreach
输出
one
分享我的文章[url=http://zgqhyh.iteye.com/blog/469033]程序员赚钱之路探索[/url]
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
分享我的文章[url=http://zgqhyh.iteye.com/blog/469033]程序员赚钱之路探索[/url]
本文详细介绍了PHP中的循环结构,包括while、do...while、for和foreach等四种主要类型的使用方法及示例代码。通过实例演示了如何用这些循环来实现重复执行代码块。
1240

被折叠的 条评论
为什么被折叠?



