What are the benefits of using for loops instead of foreach loops in PHP, and in what scenarios are they more suitable?

Using for loops instead of foreach loops in PHP can be beneficial when you need to have more control over the iteration process or when you need to work with the index of the array. For loops are more suitable in scenarios where you need to perform specific actions based on the index or need to manipulate the index during the iteration process.

// Using a for loop instead of a foreach loop to iterate over an array
$colors = array("red", "green", "blue");

for ($i = 0; $i < count($colors); $i++) {
    echo "Color at index $i is: " . $colors[$i] . "\n";
}