超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<流程控制的替代语法do..while>
Last updated: Fri, 22 Jun 2007

while

while 循环是 PHP 中最简单的循环类型。它和 C 语言中的 while 表现得一样。while 语句的基本格式是:

while (expr) statement

while 语句的含意很简单,它告诉 PHP 只要while 表达式的值为 TRUE 就重复执行嵌套中的循环语句。表达式的值在每次开始循环时检查,所以即使这个值在循环语句中改变了,语句也不会停止执行,直到本次循环结束。有时候如果 while 表达式的值一开始就是 FALSE,则循环语句一次都不会执行。

if 语句一样,可以在 while 循环中用花括号括起一个语句组,或者用替代语法:

while (expr): statement ... endwhile;

下面两个例子完全一样,都显示数字 1 到 10:

<?php
/* example 1 */

$i = 1;
while (
$i <= 10) {
   print
$i++;  /* the printed value would be
                   $i before the increment
                   (post-increment) */
}

/* example 2 */

$i = 1;
while (
$i <= 10):
   print
$i;
  
$i++;
endwhile;
?>




add a note add a note User Contributed Notes
while
yohgaki at hotmail dot com
01-Apr-2001 05:09
If you want to traverse array, foreach() is faster than while() a little.
[Benched with PHP4.0.4pl1/Apache DSO/Linux]

i.e.
foreach ($array as $k => $v)
is a little faster than
while (list($k,$v) = each($array))

You might want to use foreach for large arrays.

<流程控制的替代语法do..while>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22