What is the best practice for accessing array elements in a foreach loop to prevent index errors in PHP?

When accessing array elements in a foreach loop in PHP, it's important to ensure that the array key exists before trying to access it to prevent index errors. One way to do this is by using the `isset()` function to check if the key exists before accessing the element. This helps avoid errors when iterating over arrays with potentially missing or undefined keys.

$myArray = [1, 2, 3, 4, 5];

foreach ($myArray as $key => $value) {
    if (isset($myArray[$key])) {
        // Access the array element safely
        echo $myArray[$key] . "\n";
    }
}