聯(lián)系官方銷售客服
1835022288
028-61286886
while 循環(huán)是 PHP 中最簡單的循環(huán)類型。它和 C 語言中的 while 表現(xiàn)地一樣。while 語句的基本格式是:
while (expr) statement
while 語句的含意很簡單,它告訴 PHP 只要 while 表達(dá)式的值為 true 就重復(fù)執(zhí)行嵌套中的循環(huán)語句。表達(dá)式的值在每次開始循環(huán)時檢查,所以即使這個值在循環(huán)語句中改變了,語句也不會停止執(zhí)行,直到本次循環(huán)結(jié)束。 如果 while 表達(dá)式的值一開始就是 false,則循環(huán)語句一次都不會執(zhí)行。
和 if 語句一樣,可以在 while 循環(huán)中用花括號括起一個語句組,或者用替代語法:
while (expr): statement ... endwhile;
下面兩個例子完全一樣,都顯示數(shù)字 1 到 10:
<?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): print $i; $i++; endwhile; ?>