Home > Software engineering >  PHP Warning: Undefined array key (isset)
PHP Warning: Undefined array key (isset)

Time:10-23

I receive this error inside the error_log. When I do a search on Google they recommend adding "isset" function.

PHP Warning:  Undefined array key "h" in /path-to-script/script.php on line 452

This is line 452:

if ($maxitems == 0 || ($count['h'] > $min && $count['h'] < $max)) $b['h'] .= '<div >... {html}

I tried to add "isset" the following way:

if ($maxitems == 0 || (isset($count['h']) > $min && isset($count['h']) < $max)) $b['h'] .= '<div >...

After adding "isset" the error goes away, but the items don't show on the webpage, so basically it breaks something.

It is a PHP script to gather some JSON via API and show the items on the web.

Any ideas what could be wrong?

Thanks!

CodePudding user response:

Use isset() in a way something like this

if ($maxitems == 0 || (isset($count['h']) && $count['h'] > $min && $count['h'] < $max)) {
    $b['h'] .= '<div >... {html} ... </div>';
}

CodePudding user response:

Change:

if ($maxitems == 0 || ($count['h'] > $min && $count['h'] < $max))

To:

if ($maxitems == 0 || (isset($count['h']) && $count['h'] > $min && $count['h'] < $max))
  • Related