小白教程
所有教程
关于
Search
172.70.131.190
172.70.131.190
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“PHP While 循环”的源代码
本页内容
上一节:
PHP_数组排序
下一节:
PHP_For_循环
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:PHP While 循环}}[[Category:PHP 教程|18]] = PHP 循环 - While 循环 = 循环执行代码块指定的次数,或者当指定的条件为真时循环执行代码块。 == PHP 循环 == 在您编写代码时,您经常需要让相同的代码块一次又一次地重复运行。我们可以在代码中使用循环语句来完成这个任务。 在 PHP 中,提供了下列循环语句: * '''while''' - 只要指定的条件成立,则循环执行代码块 * '''do...while''' - 首先执行一次代码块,然后在指定的条件成立时重复这个循环 * '''for''' - 循环执行代码块指定的次数 * '''foreach''' - 根据数组中每个元素来循环代码块 == while 循环 == while 循环将重复执行代码块,直到指定的条件不成立。 === 语法 === <sample title="" desc="" lang="php" hererun="1"> while (条件) { 要执行的代码; } </sample> == 下面的实例首先设置变量 i 的值为 1 ($i=1;)。 然后,只要 i 小于或者等于 5,while 循环将继续运行。循环每运行一次,i 就会递增 1: <sample title="" desc="" lang="php" hererun="1"> <html> <body> <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br>"; $i++; } ?> </body> </html> </sample> 输出: <pre> The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 </pre> == do...while 语句 == do...while 语句会至少执行一次代码,然后检查条件,只要条件成立,就会重复进行循环。 === 语法 === <sample title="" desc="" lang="php" hererun="1"> do { 要执行的代码; } while (条件); </sample> == 下面的实例首先设置变量 i 的值为 1 ($i=1;)。 然后,开始 do...while 循环。循环将变量 i 的值递增 1,然后输出。先检查条件(i 小于或者等于 5),只要 i 小于或者等于 5,循环将继续运行: <sample title="" desc="" lang="php" hererun="1"> <html> <body> <?php $i=1; do { $i++; echo "The number is " . $i . "<br>"; } while ($i<=5); ?> </body> </html> </sample> 输出: <pre> The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 </pre> for 循环和 foreach 循环将在下一章进行讲解。
返回至“
PHP While 循环
”。
上一节:
PHP_数组排序
下一节:
PHP_For_循环