PHP Notice: Undefined offset: 1 (Array)

Posted on September 9, 2019 at 2:06 pm

How to fix PHP Notice: Undefined offset: 1?

This PHP snippet will generate a “Undefined Offset” notice:

$parts = explode(".", "aaaa");
 
$data = $parts[1];
 
print_r($data);

This happens because $parts[1] is undefined.

To fix this “Undefined Offset” notice just check if isset($parts[1]):

$parts = explode(".", "aaaa");
 
$data = isset($parts[1]) ? $parts[1] : 0;
 
print_r($data);

That’s all!

Receive updates via email

Other Posts

Updated Posts