Home  >  Article  >  How to use php while loop

How to use php while loop

无忌哥哥
无忌哥哥Original
2018-06-28 10:08:112817browse

* while loop

* 1. Entry judgment: while(condition){ #code }

* 2. Exit judgment: do { #code } while(condition)

echo &#39;<h3>while循环</h3><hr color="green">&#39;;
for ($i=0; $i<10; $i++) {
echo $i<9 ? $i.&#39;,&#39; : $i;
}
echo &#39;<hr>&#39;;

//Use while to implement the above functions

//1. Entry judgment: judge whether the condition is successful before entering the loop, it is possible not to do it once

$i = 0; //初始条件
// $i = 10; //初始条件改为10,一次都不会执行
while ($i<10) {  //循环条件
echo $i<9 ? $i.&#39;,&#39; : $i;
$i++;  //更新条件,如果没有将进和死循环
}
echo &#39;<hr>&#39;;

//2 .Exit judgment: judge whether the condition is successful after the loop execution ends, and execute the loop body at least once

$i = 0; //初始条件
// $i = 10; //初始条件改为20,但仍会执行一次
do { 
echo $i<9 ? $i.&#39;,&#39; : $i;
$i++;  //更新条件,如果没有将进和死循环
} while ($i<10);  //循环条件
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn