Skip to content
PHP Logo

What is the purpose of using PHP foreach loop with an array in the given code snippet?

In the given code snippet, the purpose of using a PHP foreach loop with an array is to iterate over each element in the array and perform a certain operation on each element. This is a common practice when you need to process each element of an array individually without having to manually write code for each element. Code snippet:

php
```php
$colors = array("red", "green", "blue");

foreach ($colors as $color) {
    echo $color . "<br>";
}
```

In this code snippet, the foreach loop iterates over the $colors array and prints each color on a new line. This allows for easy processing of each element in the array without the need for manual indexing or iteration.