PHP development basic tutorial loop statement
1. PHP loop
When we write code, we often need to make the same code block run again and again. At this time we can use loop statements in the code to complete 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 through the code block a specified number of times
foreach - loops through the code block based on each element in the array
2. 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;
}
Let’s get to know the while loop through a code logic diagram
First determine whether the condition is met. If it is met, the code in the curly brackets will be executed until the condition is not met. Jump out
Example 1 loops to output integers from 1 to 50 : The source code is as follows
<?php $i=1; while($i<=50){ echo $i." "; $i++; } ?>
Example 2: Output a table from 1 to 100 to realize the interlaced color changing function
First output the entire table: The source code is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php //定义循环的初始值 $i=0; //输出表格 echo '<table width="800" border="1" >';//双引号外面要套单引号,双引号会报错 while($i<100){ //0 - 9 为一行 //10 -19 为一行 //因此,每一行的开始都能够被10求余后为零,如为10的时候,应该显示行开始的标签 if($i%10==0){ //为了隔行变色,第2,4,6每行的颜色变过色的,因此我们又可以再进行一次取余运算 if($i%20==0){ //第1,3,5等行正常输出 echo "<tr>"; }else{ //第2,4,6等行正常输出 echo '<tr bgcolor="pink">';//双引号外面要套单引号,双引号会报错 } } echo "<td>"; echo $i; echo "</td>"; $i++; //同理,每一行结束是不是应该有一个</tr>结束标签呢? if($i%10==0){ echo "</tr>"; } } echo "</table>"; ?> </body> </html>
Note: Please refer to the relevant parts of the HTML course for the table part only
Note: In the statement, double quotes must be enclosed in single quotes, and single quotes must be enclosed in double quotes
3. do...while loop
Syntax: do
{
Code to be executed;
}
while (condition);
l 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
l do ...The difference between while and while is that their values are checked at different times.
l do-while Regardless of whether the while judgment is true, the code block loop statement is executed once, and it is guaranteed to be executed once (the truth value of the expression is checked after each loop).
However, our previous while loop will check the Boolean judgment area and execute it if it is true. If not established, it will not be executed.
<?php $i = 0; do { echo $i; } while ($i > 0); ?>
In the above code, $i is definitely not greater than 0, and it is also executed.
Of course, if you don’t understand it yet, it doesn’t matter if you really can’t think of the application scenarios. You can skip this block completely.
do...while is rarely used. We may use it in resource processing such as file opening, etc.
4. For loop control statement
The for loop is used when the number of times the script needs to be run is known in advance
Syntax :
for (initial value; condition; increment)
{
Code to be executed;
}
Parameters:
-
Initial value: Mainly initializes a variable value, used 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 incremental parameters can be empty, or have multiple expressions (separated by commas)
Example: Use a for loop to output 1-100. The source code is as follows
<?php for($i=1;$i<=50;$i++){ echo $i." "; } ?>
Example: Use a for loop to type the 9*9 multiplication table
<?php //99乘法口诀表从1开始,所以声明一个变量$i = 1,让$i小于10,也就是最大值为9 for($i=1;$i<=9;$i++){ //1x1=1,2x2等于4,所以第二次循环的最大值为$i的值,因此$j=1, $j在循环自加的过程当中,只能够小于等于$i for($j=1;$j<=$i;$j++){ echo $i."x".$j."=".$i*$j.' '; } //每行结束输出一个换行 echo "<br/>"; } ?>
Note: The code is output horizontally , the newline character is executed once after each inner for loop ends
Let’s add a few similarities and differences that pop out
Statement | Function |
exit | exitWe talked about it before, stopping subsequent execution from the current location |
break | Encountered it before, jump out of the loop or jump out of the structure to execute the subsequent code |
continue | ## Break out of this loop and continue the next loop |
<?php for ($i = 1; $i <= 10; $i++) { if($i == 4){ //待会儿换成contiune试试 break; } echo '学习PHP的第'.$i.'天,加油<br />'; } ?>Replace break with continue and try to check the results: (line 4 is lost, other lines are output normally)
5. foreach loop (the array has not been introduced here yet, you can read it after reading it) Study this chapter after the introduction of arrays)
The foreach loop is used to traverse the arrayThe syntax is: foreach ($array as $value){
To execute the code;
}
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <body> <?php $x=array(1,2,3,4); foreach ($x as $value) { echo $value . "<br>"; } ?> </body> </html>