What is the usage of php do while statement

藏色散人
Release: 2023-03-12 13:32:02
Original
2589 people have browsed it

php The do while statement will execute the code at least once, and then check the condition. As long as the condition is true, the loop will be repeated; the syntax of the do while statement is "do{code to be executed;}while (condition) ;".

What is the usage of php do while statement

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

What is the usage of php do while statement ?

do...while - First execute the block of code once, then repeat the loop when the specified condition is true

do...while statement

The do...while statement will execute the code at least once, then check the condition, and repeat the loop as long as the condition is true.

Syntax

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

Example

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

Then, start the do...while loop. The loop increments the value of variable i by 1 and then outputs. First check the condition (i is less than or equal to 5), as long as i is less than or equal to 5, the loop will continue to run:

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

Output:

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

The do-while loop is very similar to the while loop, the difference is The reason is that the value of the expression is checked at the end of each loop rather than at the beginning. The main difference from a normal while loop is that the do-while loop statement is guaranteed to be executed once (the truth value of the expression is checked after each loop), but in a normal while loop this is not necessarily the case (the truth value of the expression Checked at the beginning of the loop, if it is false at the beginning, the entire loop terminates immediately).

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the usage of php do while statement. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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!