Are there any best practices for using 'foreach' in PHP?

When using foreach loops in PHP, it is important to ensure that the array being iterated over is not modified within the loop. Modifying the array while iterating over it can lead to unexpected behavior or errors. To avoid this issue, you can create a copy of the array before iterating over it using foreach.

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

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