How does PHP handle array pointers when using foreach loops?

When using foreach loops in PHP, the array pointer is automatically moved to the next element after each iteration. This means that if you need to access the current element's index within the loop, you should use a reference variable to store the key value. This way, the array pointer will not be affected by the loop.

$myArray = [1, 2, 3, 4, 5];
foreach ($myArray as $key => &$value) {
    // $key will hold the current index, and $value will hold the current element
    echo "Index: $key, Value: $value" . PHP_EOL;
}