PHP: Avoid undefined index?

前端 未结 9 1890
闹比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 12:00

    With PHP 7, the null coalescing operator can be used to deal with optional request variables. You can change your $_POST['product'] reference to $_POST['product'] ?? null which will resolve to null (rather than throwing the warning) if 'product' is not a valid key in the post array. If you wanted to check both the $_POST and $_GET arrays for a value, you would use $_POST['product'] ?? $_GET['product'] ?? null.

提交回复
热议问题