What are potential pitfalls when accessing array indexes in PHP scripts?

One potential pitfall when accessing array indexes in PHP scripts is not checking if the index actually exists before trying to access it. This can lead to "Undefined index" notices and potentially cause your script to break. To avoid this issue, you can use the isset() function to check if the index exists before accessing it.

// Potential pitfall: accessing array index without checking if it exists
$array = ['foo' => 'bar'];

// This can lead to an "Undefined index" notice
$value = $array['baz'];

// Fix: Check if the index exists before accessing it
if (isset($array['baz'])) {
    $value = $array['baz'];
} else {
    // Handle the case where the index doesn't exist
    $value = 'default value';
}