PHP: Avoid undefined index?

前端 未结 9 2060
闹比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:55

    You could also try

    $product = (isset($_POST['product'])) ? $_POST['product'] : null;
    

    This will set $product to the $_POST value if it exists, or to null if not. Then you could try

    if ($product) {
      do_something();
    }
    

    or access your array with

    $products[$product];
    

    I feel this way it makes your code that little bit easier on the eyes..

提交回复
热议问题