How can the issue of undefined offset be resolved when iterating through an array in PHP?
When iterating through an array in PHP, the issue of undefined offset occurs when trying to access an index that does not exist in the array. This can be resolved by checking if the offset exists before trying to access it using isset() or array_key_exists() functions. By doing this, you can prevent the "Undefined offset" error from being thrown.
$array = [1, 2, 3, 4, 5];
for ($i = 0; $i < count($array); $i++) {
if (isset($array[$i])) {
echo $array[$i] . "\n";
}
}