php学习之旅 (5) 循环

本文详细介绍了PHP中的循环结构,包括while、do...while、for和foreach等四种主要类型的使用方法及示例代码。通过实例演示了如何用这些循环来实现重复执行代码块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

分享我的文章[url=http://zgqhyh.iteye.com/blog/469033]程序员赚钱之路探索[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值