Home > Backend Development > PHP Tutorial > Examples introduce the four loops of while, do...while, for, and foreach

Examples introduce the four loops of while, do...while, for, and foreach

伊谢尔伦
Release: 2023-03-11 08:20:01
Original
1552 people have browsed it

#while loop in php, loops to execute the code block a specified number of times, or loops to execute the code block when the specified condition is true.
----------------------------------------- -----------------------

When we write code, we often need to repeat a block of code multiple times. We can use the while loop statement to complete this task.

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

do...while--First execute the code block once, and then repeat the loop when the specified condition is true.
for--The number of times the loop executes the code block.

foreach
--Loop the code block based on each element in the array.

while loopThe while statement loops through the code block until the specified condition is not true.

while(condition)

{

Code to be executed in a loop;
}

Case: Set the value of a variable a to 11 ($a=11).

Then, as long as a
---------------------------------- -----------------------

$a=11;
while($a<=20){
 echo "输出数值:".$a."";
 $a++;
}
Copy after login

will output the following results: while output value: 11

while output value: 12

while output value: 13

while output value: 14
while output value: 15
while output value: 16
while output value: 17
while output value: 18
while output value: 19
while output value: 20

The following is the example code:

<?php
$a=11;
while($a<=20){
 echo "while输出数值:".$a."<br>";
 $a++;
}
?>
while循环Code、
Copy after login

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

Syntax

do

{

Code to be executed;
}
while(condition)

Example

The following example first sets the value of variable a to 1 ($a=11). Then, start the do...while loop. The loop increments the value of variable a by 1 and then outputs it. First check the condition (a is light rain or equal to 20). As long as a is less than or equal to 5, the loop will be executed accordingly:


do...while output value: 11

do...while output Value: 12

do...while Output value: 13
do...while Output value: 14
do...while Output value: 15
do...while Output value: 16
do...while output value: 17
do...while output value: 18
do...while output value: 19
do...while output value: 20

The following is an example code:

<?php
$a=11;
do{
 echo"do...while输出数值:".$a."<br/>";
 $a++;
}while($a<=20);
?>
do...while循环代码
Copy after login

for loopLoop to execute the code block the specified number of times , or loop through a block of code when a specified condition is true.

The for loop is used when you know in advance the number of times the script needs to run.

Syntax

for(initial value; conditional increment; ){

Code to be executed

}

Initial value: Mainly initializes a variable value to set a counter (but it can be any code that is executed once at the beginning of the loop).

Conditions: Restrictions on loop execution. If TRUE, the loop continues. If FALSE, the loop ends.

Increment: Mainly used to increment the counter (but can be any code that is executed at the end of the loop).

Note: The above initial value and increment parameters can be empty, or have multiple

expressions

(separated by commas).

Example

for output value: 11

for output value: 12

for output value: 13
for output value: 14
for output value: 15
for output value: 16
for output value: 17
for output value: 18
for output value: 19
for output value: 20

The following is the example code:

<?php
 for($a=11;,$a<=20;,$a++){
  echo "for输出数值:".$a."<br/>";
 }
 ?>
Copy after login

foreach loopthe foreach loop is used to

traverse the array

. Syntax

foreach ($array as $value){

To execute the code;
}

Every time the loop is performed, the value of the current array will be assigned to $ value variable (the array pointer will move one by one), and on the next loop, you will see the next value in the array.

Example

The following

example demonstrates

a loop that outputs the values ​​of a given array: Outputs the array values ​​one by one :one

Output the array values ​​one by one: two

Output the array values ​​one by one: three
Output the array values ​​one by one: four
Output the array values ​​one by one: five

The following is an example Code:

<?php
 $x=array("one","two","three","four","five");
 foreach($x as $value){
  echo "逐一输出数组值:".$value."<br/>";
 }
 ?>
Copy after login

The above is the detailed content of Examples introduce the four loops of while, do...while, for, and foreach. 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