In what situations should foreach loops be preferred over for loops in PHP?
Foreach loops should be preferred over for loops in PHP when iterating over arrays or collections where you only need to access the values and not the keys. Foreach loops provide a cleaner and more concise syntax for iterating through arrays without needing to manually manage the array pointer or index.
// Example of using foreach loop to iterate over an array
$colors = ["red", "green", "blue"];
foreach ($colors as $color) {
echo $color . "<br>";
}