Home >Backend Development >PHP Problem >How to implement while loop statement in PHP

How to implement while loop statement in PHP

藏色散人
藏色散人Original
2023-01-18 09:15:241887browse

There are two syntaxes for PHP to implement while loop statements, namely: 1. "while (...) {...}" syntax, which means that as long as the specified condition is true, the while loop will be executed. Code block; 2. "do {...} while (...);" syntax, which will first execute the code block once, then check the condition, and repeat the loop if the specified condition is true.

How to implement while loop statement in PHP

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

How to implement the while loop statement in PHP?

PHP while loop executes a block of code when a specified condition is true.

PHP Loops

When you write code, you often need to run the same block of code over and over again. Instead of adding several nearly equal lines of code to the script, we can use a loop to perform such a task.

In PHP, we have the following loop statement:

  • while - As long as the specified condition is true, the block of code is looped

  • do...while - Execute the code block once, and then repeat the loop as long as the specified condition is true

  • for - Loop the code block a specified number of times

  • foreach - Iterate through each element in an array and loop through a block of code

PHP while loop

As long as the specified condition is true , the while loop will execute the code block.

Syntax

while (条件为真) {
  要执行的代码;
}

The following example first sets the variable $x to 1 ($x=1). The while loop is then executed as long as $x is less than or equal to 5. Each time the loop runs, $x will be incremented by 1:

Instance

<?php 
$x=1; 
while($x<=5) {
  echo "这个数字是:$x <br>";
  $x++;
} 
?>

PHP do...while loop

do...while loop The code block is executed once, then the condition is checked, and if the specified condition is true, the loop is repeated.

Syntax

do {
  要执行的代码;
} while (条件为真);

The following example first sets the variable $x to 1 ($x=1). Then, the do while loop outputs a string and then increments the variable $x by 1. The condition is then checked (whether $x is less than or equal to 5). The loop will continue to run as long as $x is less than or equal to 5:

Example

<?php 
$x=1; 
do {
  echo "这个数字是:$x <br>";
  $x++;
} while ($x<=5);
?>

Note that the do while loop only tests the condition after the statements within the loop are executed. This means that the do while loop will execute the statement at least once, even if the conditional test fails the first time.

The following example sets $x to 6, then runs the loop, and then checks the conditions:

Example

<?php 
$x=6;
do {
  echo "这个数字是:$x <br>";
  $x++;
} while ($x<=5);
?>

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to implement while loop statement in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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