What is the potential cause of the error "Notice: Undefined offset" in PHP when iterating through an array?

The "Notice: Undefined offset" error in PHP occurs when trying to access an index in an array that does not exist. This can happen when iterating through an array and trying to access an index that is beyond the array's size. To solve this issue, you should always check if the index exists before trying to access it.

// Example of checking if the index exists before accessing it in a loop
$array = [1, 2, 3, 4, 5];

for ($i = 0; $i < count($array); $i++) {
    if (isset($array[$i])) {
        echo $array[$i] . "\n";
    }
}