What is the purpose of using the foreach loop in PHP and how is it commonly used?

The purpose of using the foreach loop in PHP is to iterate over arrays or objects to access each element within them. It is commonly used when you want to perform a specific action on each element of an array without having to manually manage the iteration process.

// Example of using foreach loop to iterate over an array and output each element
$colors = array("red", "green", "blue");

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