国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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

15.png

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."&nbsp";
	$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
//定義循環(huán)的初始值
$i=0;
//輸出表格
echo '<table width="800" border="1" >';//雙引號外面要套單引號,雙引號會報錯
while($i<100){
	 //0 - 9 為一行
        //10 -19 為一行
        //因此,每一行的開始都能夠被10求余后為零,如為10的時候,應(yīng)該顯示行開始的標(biāo)簽
	if($i%10==0){
		//為了隔行變色,第2,4,6每行的顏色變過色的,因此我們又可以再進(jìn)行一次取余運(yùn)算
		if($i%20==0){
			//第1,3,5等行正常輸出
			echo "<tr>";
		}else{
			//第2,4,6等行正常輸出
			echo '<tr bgcolor="pink">';//雙引號外面要套單引號,雙引號會報錯
		}	
	}
		echo "<td>";
		echo $i;
		echo "</td>";
	$i++;
	 //同理,每一行結(jié)束是不是應(yīng)該有一個</tr>結(jié)束標(biāo)簽?zāi)兀?
	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."&nbsp";
}
?>

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,所以第二次循環(huán)的最大值為$i的值,因此$j=1, $j在循環(huán)自加的過程當(dāng)中,只能夠小于等于$i
	for($j=1;$j<=$i;$j++){
		echo $i."x".$j."=".$i*$j.'&nbsp;&nbsp;&nbsp;';
	}
	//每行結(jié)束輸出一個換行
	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


Let’s take a look at an example. The source code is as follows:

<?php
for ($i = 1; $i <= 10; $i++) {
    if($i == 4){
            //待會兒換成contiune試試
            break;
    }
    echo '學(xué)習(xí)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 array

The syntax is:

foreach ($array as $value)

{
To execute the code;
}

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). When looping once, you will see the next value in the array

Example: The source code is as follows

<!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>
Continuing Learning
||
<?php $i=1; while($i<=50){ echo $i." "; $i++; } ?>
submitReset Code