PHP basics: Introduction to the usage of while statement in PHP

不言
Release: 2023-04-04 15:28:01
Original
5805 people have browsed it

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.

PHP basics: Introduction to the usage of while statement in PHP

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(条件表达式){ 
执行处理
增加/减少的表达式; 
}
Copy after login

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++; 
} 
?>
Copy after login

The running results are as follows:

PHP basics: Introduction to the usage of while statement in PHP

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!