In this tutorial, you shall learn how to solve PHP error “Notice: Undefined offset: 0” when working with arrays.

PHP Array – Notice: Undefined offset: 0

You will get PHP message “Notice: Undefined offset” when you try to access an element with an offset or index that is not present in the PHP Array. We shall go through the scenarios where this could happen, and discuss how to solve it.

1. Accessing Array with Default Offset

For example, consider the following PHP program.

</>
Copy
<?php
$arr = array(25, 87, 63);
echo $arr[4];
?>

We have defined an array with some numbers. As we have not specified any offset to the elements, the default offset will start from 0 and would be 0,1 and 2 for the three elements respectively.

The offset range is from 0 to 2, but we are trying to access element of $arr at offset 4. This could echo a Notice message.

When you run the above program, you will get the following message displayed in the browser.

Notice: Undefined offset: 4 “