PHP foreach loop

巴扎黑
Release: 2016-11-12 09:49:48
Original
1234 people have browsed it


PHP foreach loop

foreach loop only works on arrays and is used to iterate over each key/value pair in the array.

Syntax

foreach ($array as $value) {

code to be executed;

}

Every time a loop iteration is performed, the value of the current array element will be assigned to the $value variable, and the array pointer will Move one by one until you reach the last array element.

The following example demonstrates a loop that will output the values ​​of a given array ($colors):

Example

<?php 
$colors = array("red","green","blue","yellow"); 
foreach ($colors as $value) {
  echo "$value <br>";
}
?>
Copy after login


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