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:

  "; } ?>  

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.

'; echo $num[9].'
'; //我们可以得到数组中元素的总个数,为10 echo count($num); //遍历这个索引数组的话,我们就可以定义一个变量为$i //$i 的值为0,从0开始 //可以设定一个循环条件为:$i 在下标的(9)最大值之内循环 for($i = 0; $i < count($num); $i++) { echo $num[$i].'
'; } ?>

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:

  "; } ?>  

Output:

one
two
three

We can traverse the continuous index array through foreach

   '中国', 100 => '美国', 20=> '韩国', 300 => '德国', ); foreach($cou as $key => $value) { echo $key . '------' . $value .'
'; } ?>

Output:

0------China
100 ------United States
20------Korea
300------Germany

If there are arrays in the array, how should we traverse the loop?

 array( '中国' => 'china', '美国' => 'usa', '德国' => ' Germany', ), 1 => array( '湖北' => 'hubei', '河北' => 'hebei', '山东' => 'shandong', '山西' => 'sanxi', ), ); //注:我们在使用foreach循环时,第一次循环将键为0和键为1的两个数组赋值给一个变量($value)。 //然后,再套一个循环遍历这个$value变量,$value中的值取出来,赋值给$key和$v。 foreach($data as $value){ //第一次循环把国家的数组赋值给了$value //第二次循环把中国的省份的数组又赋值给了$value //因此,在循环的时候把$value再遍历一次 foreach($value as $key => $v) { echo $key . '-----' . $v .'
'; } //为了看的更清晰,在中间加上华丽丽的分割线方便你来分析 echo '----------分割线-----------
'; } ?>


Output:

中国-----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.


Continuing Learning
||
"; } ?>
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!