PHP For Loop
Loop through a code block a specified number of times, or when a specified condition is true.
for loop
Syntax
for (initial value; condition; increment)
{
Code to be executed;
}
Parameters:
? Initial value: It is an initialization assignment, and multiple codes can be assigned at the same time.
? Condition: Evaluate before each loop starts. If the value is TRUE, the loop continues and the nested loop statement is executed. If the value is FALSE, the loop is terminated.
? Increment: Evaluated after each loop.
Note: The above initial value and increment parameters can be empty, or have multiple expressions (separated by commas).
Example
The following example defines a loop with an initial value of i=1. As long as the variable i is less than or equal to 5, the loop will continue to run. Each time the loop runs, the variable i will be incremented by 1:
<html> <body> <?php for ($i=1; $i<=5; $i++) { echo "The number is" . $i . "<br>"; } ?> </body> </html>
Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
We learned about arrays in the previous chapter, and the for loop is a simple counting loop, and the subscript of the index array is an integer value. Therefore, we can iterate through the index array through a for loop.
<?php //聲明一個(gè)數(shù)組,值為1到10 $num = array(1,2,3,4,5,6,7,8,9,10); //按照索引數(shù)組的特點(diǎn),下標(biāo)從0開(kāi)始。所以1的下標(biāo)為0,10的下標(biāo)為9 echo $num[0].'<br />'; echo $num[9].'<br />'; //我們可以得到數(shù)組中元素的總個(gè)數(shù),為10 echo count($num); //遍歷這個(gè)索引數(shù)組的話,我們就可以定義一個(gè)變量為$i //$i 的值為0,從0開(kāi)始 //可以設(shè)定一個(gè)循環(huán)條件為:$i 在下標(biāo)的(9)最大值之內(nèi)循環(huán) for($i = 0; $i < count($num); $i++) { echo $num[$i].'<br />'; } ?>
Through the above example, we have looped the array.
Because the subscript starts from 0, define $i=0. Let $i increase by 1 each time it loops, but it must be less than 10, because the maximum value of the array subscript is 9.
foreach loop
The foreach loop is used to traverse the array.
Syntax
foreach ($array as $value)
{
To execute the code;
}
array represents an array variable, in each When the loop is executed, the value of each element will be temporarily assigned to the variable value, and the value of value obtained by the code statement to be executed is different each time.
Another syntax
foreach ($array as $key => $value)
{
To execute code;
}
key represents the subscript of the array, and value represents the value of the array. So for a numeric subscript array, the value of key in each loop is the number that starts from 0 and grows.
Example
The following example demonstrates a loop that outputs the values ??of a given array:
<html> <body> <?php $x=array("one","two","three"); foreach ($x as $value) { echo $value . "<br>"; } ?> </body> </html>
Output:
one
two
three
We can traverse the continuous index array through foreach
<html> <body> <?php $cou = array( 0 => '中國(guó)', 100 => '美國(guó)', 20=> '韓國(guó)', 300 => '德國(guó)', ); foreach($cou as $key => $value) { echo $key . '------' . $value .'<br/>'; } ?> </body> </html>
Output:
0------China
100 ------United States
20------Korea
300------Germany
If there are arrays in the array, how should we traverse the loop?
<?php $data = array( 0 => array( '中國(guó)' => 'china', '美國(guó)' => 'usa', '德國(guó)' => ' Germany', ), 1 => array( '湖北' => 'hubei', '河北' => 'hebei', '山東' => 'shandong', '山西' => 'sanxi', ), ); //注:我們?cè)谑褂胒oreach循環(huán)時(shí),第一次循環(huán)將鍵為0和鍵為1的兩個(gè)數(shù)組賦值給一個(gè)變量($value)。 //然后,再套一個(gè)循環(huán)遍歷這個(gè)$value變量,$value中的值取出來(lái),賦值給$key和$v。 foreach($data as $value){ //第一次循環(huán)把國(guó)家的數(shù)組賦值給了$value //第二次循環(huán)把中國(guó)的省份的數(shù)組又賦值給了$value //因此,在循環(huán)的時(shí)候把$value再遍歷一次 foreach($value as $key => $v) { echo $key . '-----' . $v .'<br />'; } //為了看的更清晰,在中間加上華麗麗的分割線方便你來(lái)分析 echo '----------分割線-----------<br />'; } ?>
Output:
中國(guó)-----china
United States-----usa
Germany----- Germany
----------Separation Line----- ------
Hubei-----hubei
Hebei-----hebei
Shandong-----shandong
shanxi-----sanxi
- ---------Dividing line-----------
Summary:
First During the second loop, the array is assigned to $value, and then foreach is used to loop over $value. Give the key in the two-dimensional subarray to $key and assign the value to the variable $v.
The first loop exits the loop of the sub-array, and the subsequent code is executed to display the dividing line.
And so on, the same is true for the second cycle.