The PHP foreach Loop

The foreach loops work only on arrays. You can use it to loop through each key and its value pairs in an array.

Example

[php]<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
echo "$value <br>";
}
?> [/php]

In the above statement, we have mentioned the variable $colors pointing to the array fields. Then we are using a foreach loop to execute the statement.

The above will generate the following result.

red
green
blue
yellow

Related Posts

Leave a Reply