PHP: Avoid undefined index?

前端 未结 9 2033
闹比i
闹比i 2021-01-17 11:18

Every time a POST value is not equal to the list of values set in an array will return: Undefined Index error, I made an if statement but is not working.

Here\'s the

9条回答
  •  日久生厌
    2021-01-17 11:58

    I'm a bit confused by your code. It looks like your array has the same key and value, so:

    $products['saucepan'] = 'saucepan'
    

    Perhaps you are trying to do this, which will check whether the product exists in the products array:

    if(isset($_POST['product']) && array_key_exists($_POST['product'], $products))
    {
      // do stuff
    }
    else
    {
      echo "This item is not available";
    }
    

提交回复
热议问题