What potential pitfalls should be considered when using foreach loops to iterate through arrays in PHP?

One potential pitfall when using foreach loops to iterate through arrays in PHP is modifying the array being iterated within the loop. This can lead to unexpected behavior or errors. To avoid this issue, consider creating a copy of the array before iterating through it.

// Create a copy of the array before iterating through it
$array = [1, 2, 3, 4, 5];
$arrayCopy = $array;

foreach ($arrayCopy as $value) {
    // Do not modify the original array within the loop
    echo $value . " ";
}