What are the advantages of using foreach loops over for loops when traversing arrays in PHP?

When traversing arrays in PHP, using foreach loops is often preferred over for loops because foreach loops automatically handle the iteration over each element of the array without the need to manually manage an index variable. This can lead to cleaner and more concise code, as well as reducing the risk of off-by-one errors. Additionally, foreach loops are more readable and intuitive, making the code easier to understand for other developers.

// Using foreach loop to traverse an array
$colors = ['red', 'green', 'blue'];

foreach ($colors as $color) {
    echo $color . "\n";
}