while语句是最简单的反复语句。在满足特定条件的情况,需要反复实行特定命令语句时使用反复语句。
while语句构造
语法构造 | 说明 |
---|---|
while(expr) stmt; |
表现式的结果为TRUE中时反复实行命令语句 |
<?php
$var = 0;
while($var < 3) // expression will be TRUE till third comparison
{
echo "$var\r\n"; // statement will be executed three times
$var++; // increase $var by one
sleep(1); // 1 second delay
}
?>
[result]
0
1
2
<?php
$var = 0;
while(1) // expression will always be TRUE
{
echo "$var\r\n";
$var++; // increase $var by one, $var = 1, 2, 3, …
sleep(1); // 1 second delay
}
?>