How to set PHP not to check undefind index for $_GET when E_NOTICE is on?

前端 未结 3 1048
轻奢々
轻奢々 2021-01-26 10:53

When E_NOTICE is set to on, PHP will report undefined index for arrays. I want to suppress this error for $_GET only. Is there any way to do that other than prepend

3条回答
  •  孤街浪徒
    2021-01-26 11:13

    The best method is to check for the existence of the array key before you use it each time, using isset.

    So instead of

    $cow=(int)$_GET['cow'];
    

    do

    if(isset($_GET['cow'])){ $cow=(int)$_GET['cow']; }
    

    That's verbose, however. So, you can wrap this in a function such as

    function get($key_name){
      return isset($_GET[$key_name])?$_GET[$key_name]:null;
      }
    

    and then,

    $cow=get('cow');
    

提交回复
热议问题