Home > Article > Backend Development > How to use foreach() in php
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;}".
##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!