How can the use of foreach loops in PHP impact the efficiency of code execution?

Using foreach loops in PHP can impact the efficiency of code execution if the array being iterated over is large. This is because foreach loops require iterating over each element of the array, which can be time-consuming for large arrays. To improve efficiency, consider using other looping mechanisms like for loops, especially when the array size is known in advance.

// Example of using a for loop instead of a foreach loop for iterating over an array
$array = [1, 2, 3, 4, 5];
$arrayLength = count($array);

for ($i = 0; $i < $arrayLength; $i++) {
    echo $array[$i] . "\n";
}