PHP Loops - Understanding and Using While Loops

jacklove
Release: 2023-03-25 13:38:01
Original
1730 people have browsed it

PHP Loop - While loop plays an important role in php, this article will give some explanations about it.

PHP Loops

When you write code, you often need to have the same block of code run over and over again. We can use loop statements in our code to accomplish this task.

In PHP, the following loop statements are provided:

while - As long as the specified condition is true, the code block will be executed in a loop

do...while - Executed once block of code, and then repeats the loop when the specified condition is true

for - Loops to execute the code block a specified number of times

foreach - Based on array Loop through each element in the code block

while loop

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

Syntax

while (condition){
Code to be executed;}

Example

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

Then, as long as i is less than or equal to 5, the while loop will continue to run. 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

do...while statement

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

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 it. 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

This article provides some detailed explanations of php loops and while loops for more learning Please follow the php Chinese website to view the information.

Related recommendations:

Related knowledge and application of PHP 5 echo and print statements

Related knowledge of PHP magic variables Understanding and application

Detailed explanation of php magic variable usage examples, detailed explanation of php magic examples_PHP tutorial

The above is the detailed content of PHP Loops - Understanding and Using While Loops. 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!