What is the purpose of the foreach loop in PHP and how does it differ from a for loop?

The purpose of the foreach loop in PHP is to iterate over each element in an array or collection without needing to keep track of the index. It simplifies the process of looping through arrays and makes the code more readable. The main difference between a foreach loop and a for loop is that a foreach loop is specifically designed for iterating over arrays, while a for loop can be used for more general looping purposes.

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

// Using foreach loop to iterate over each element in the $colors array
foreach($colors as $color) {
    echo $color . "<br>";
}