What are the potential pitfalls of using traditional for loops versus foreach loops in PHP when iterating through arrays?

Using traditional for loops to iterate through arrays in PHP can lead to potential pitfalls such as off-by-one errors, increased complexity, and the risk of accidentally modifying the array structure. Using foreach loops is generally safer and more convenient for iterating through arrays in PHP.

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

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