Home > Backend Development > PHP Tutorial > Detailed explanation of 'foreach' loop statement examples of PHP loop control statements

Detailed explanation of 'foreach' loop statement examples of PHP loop control statements

怪我咯
Release: 2023-03-07 13:34:01
Original
2987 people have browsed it

This chapter introduces a special loop statement in PHP, the "foreach" loop statement.

What is the foreach loop used for?

In PHP, the foreach loop statement is specially used to loop through arrays. For arrays, you can check this article//m.sbmmt.com/php-weizijiaocheng- 360217.html

foreach loop syntax format

There are two ways to write the foreach loop syntax. The first way is as follows

foreach (array_variable as val) 
    statement;
Copy after login

array_variable represents an array variable. During each loop execution, the value of each element will be temporarily assigned to the variable val. The value of val obtained by the statement statement is different each time.

Two ways of writing

foreach (array_variable as key => val) 
    statement;
Copy after login

key represents the subscript of the array, and val 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.


foreach loop instance

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码

$a=Array("苹果","橘子","香蕉");

foreach($a as $value){

    echo $value."<br/>";
}


?>
Copy after login

Code running result:

Detailed explanation of foreach loop statement examples of PHP loop control statements

Detailed explanation of the example:

We said at the beginning that the "foreach" loop is specially used to loop arrays. So, we first define an array $a. There are three values ​​in the array. They are "apple", "orange", and "banana", and then use the foreach loop statement to loop through the array and output the values ​​in the array.


The above example is the first way of writing using foreach loop. At this time, if you want to get the $key of the array, you need to use our second way of writing, The code is as follows:

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码

$a = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "seventeen" => 17
);
foreach ($a as $key => $val) {
    echo $key .":".$val."<br/>";
}

?>
Copy after login

Code running results:

Detailed explanation of foreach loop statement examples of PHP loop control statements

The two examples above are simple applications of the two writing methods of foreach loop.



The above is the detailed content of Detailed explanation of 'foreach' loop statement examples of PHP loop control 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