Home > Article > Backend Development > PHP basics: Introduction to the usage of while statement in PHP
We introduced the content of the for loop statement in PHP in the previous article . In the next article, we will introduce another loop statement in PHP-the while loop statement. Content.
Let’s first take a brief look at the difference between while loop statements and for loop statements in PHP
If you want to specify the number of loops first, use the for statement
If you want to loop this condition first, use the while statement
Let’s write a while loop statement
The while statement will loop through all matches (true) to the conditional expression and is written in the following format.
初始值; while(条件表达式){ 执行处理 增加/减少的表达式; }
Initial values, conditional expressions and increment/decrement expressions all have this meaning.
Initial value-the first value set
Conditional expression-under what conditions we should perform loop processing
Increase/decrease expression-each processing should How many values to increase or decrease
Let’s look at a specific example
Here weset the variable name to $i
<?PHP basics: Introduction to the usage of while statement in PHP $i = 0; while($i < 5){ echo $i; $i++; } ?>
The running results are as follows:
Finally let’s take a look at Infinite loop
For example, starting from 0, if the conditional expression is not set If so, it will enter an infinite loop by continuously increasing the number with "01234567891011...100...". The execution of this program will never end, and the program will be overloaded and crash.
This article ends here. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP basics: Introduction to the usage of while statement in PHP Chinese website! ! !
The above is the detailed content of PHP basics: Introduction to the usage of while statement in PHP. For more information, please follow other related articles on the PHP Chinese website!