What are some common pitfalls to avoid when using foreach loops in PHP?

One common pitfall to avoid when using foreach loops in PHP is modifying the array being iterated over within the loop. This can lead to unexpected behavior and errors. To avoid this, make a copy of the array before iterating over it.

$originalArray = [1, 2, 3, 4, 5];
$arrayCopy = $originalArray;

foreach ($arrayCopy as $value) {
    // Do something with $value
}