In what situations should foreach loops be preferred over for loops when working with arrays in PHP?
Foreach loops should be preferred over for loops when working with arrays in PHP when you need to iterate over each element in the array without needing the index. Foreach loops provide a cleaner and more concise syntax for iterating over arrays, especially when you only need the values and not the keys.
// Using foreach loop to iterate over an array
$colors = array("red", "green", "blue");
foreach ($colors as $color) {
echo $color . "\n";
}