Home  >  Article  >  Backend Development  >  How to use foreach() in php

How to use foreach() in php

藏色散人
藏色散人Original
2020-07-25 09:03:553484browse

foreach in php only applies to arrays and is used to traverse each key-value pair in the array. The syntax of the foreach loop statement is "foreach ($array as $value) {code to be executed;}".

How to use foreach() in php

##Usage of foreach() in php:

foreach loop only applies to arrays, and used to iterate over each key/value pair in the array.

foreach ($array as $value) {code to be executed;}

Each 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 the last array element is reached.

Example:

The loop will output the value of the given array ($colors):

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

Recommended: "

PHP Tutorial"

The above is the detailed content of How to use foreach() in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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