首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

while

(PHP 4, PHP 5, PHP 7)

while loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a while statement is:

代码语言:javascript
复制
while (expr)
    statement

The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). Sometimes, if the while expression evaluates to FALSE from the very beginning, the nested statement(s) won't even be run once.

Like with the if statement, you can group multiple statements within the same while loop by surrounding a group of statements with curly braces, or by using the alternate syntax:

代码语言:javascript
复制
while (expr):
    statement
    ...
endwhile;

The following examples are identical, and both print the numbers 1 through 10:

代码语言:javascript
复制
<?php
/*?example?1?*/

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

/*?example?2?*/

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

do-while →

代码语言:txt
复制
 ? 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com