Deep understanding of PHP loop statements

黄舟
Release: 2023-03-16 15:50:01
Original
1964 people have browsed it

Loop structure
1. While loop

 while(表达式)
 {
  循环体;//反复执行,直到表达式为假
 }
Copy after login

Code:

$index = 1;
while ($index<5)
{
      print "Number is {$index}
";
     $index++;
}
print "Done";
Copy after login

Run result:
Number is 1
Number is 2
Number is 3
Number is 4
Done

2. do while loop

do {
      循环体;//反复执行,直到表达式为假
} while(表达式)
Copy after login

Code:
##

do {
         $index++;
         print "Number is {$index}
";
} while($index<0);
print "Done";
Copy after login


Run result: Number is 1
Done


Do While loop statement has certain differences with while, their difference is do while Regardless of whether the condition is true or not, it will be executed first, and while must be true before it will be executed once.

3. for loop
According to different loop conditions, there are two types of loops
One: counting loop (generally used for)
The other: conditional loop (generally used while do-while)
for (expr1; expr2; expr3) {
statement
}
expr1 is the initial value of the condition. expr2 is the condition for judgment, usually using logical operators.
The conditions for judgment. expr3 is the part to be executed after statement is executed. It is used to change the conditions for the next loop judgment, such as adding one, etc. And statement
is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted.
The following example is an example of "I won't dare to do it again" written using a for loop. It can be compared with the one using a while loop.

<?php
 for ($i=1; $i<=5; $i++) {
   echo "$i. 以后不敢了
n";
 }
 ?>
Copy after login

Run results: 1. I won’t dare anymore
2 . I won’t dare anymore
3. I won’t dare anymore
4. I won’t dare anymore
5. I won’t dare anymore


4. Foreach loop

The foreach statement is used to loop through an array. Each time the loop is performed, the value of the current array element will be assigned to the value variable (the array pointer will move one by one) - and so on

Syntax:

foreach (array as value)
{
    code to be executed;
}
Copy after login

##Code:

<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
  echo "Value: " . $value . "
";
}
?>
Copy after login

##Run result: Value: oneValue: twoValue: three

The above are four types of loop bodies in PHP. Select the corresponding loop body to use according to different conditions

The above is the detailed content of Deep understanding of PHP loop statements. 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!