Usage of php while statement

藏色散人
Release: 2023-03-05 20:50:01
Original
3687 people have browsed it

The while statement in PHP refers to the while loop statement, which is used to repeatedly execute a block of code until the specified condition is not true. Its syntax is [while (condition) {code to be executed;}].

Usage of php while statement

Recommended: "PHP Video Tutorial"

php while loop

The while loop will execute the block of code repeatedly until the specified condition is not true.

Syntax

while (条件)
{
    要执行的代码;
}
Copy after login

Example

The following example first sets the value of variable i to 1 ($i=1;).

Then, the while loop will continue to run as long as i is less than or equal to 5. Each time the loop runs, i will be incremented by 1:

<html>
<body>
<?php
$i=1;
while($i<=5)
{
    echo "The number is " . $i . "<br>";
    $i++;
}
?>
</body>
</html>
Copy after login

Output:

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Copy after login

The above is the detailed content of Usage of php while statement. 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