Avoid using isset in PHP when accessing $_POST, $_GET, and other variables?

前端 未结 6 1318
北海茫月
北海茫月 2020-12-21 07:06

how can I prevent PHP from returning an Undefined variable error every time I try to check a variable if it has contents and that certain variable hasn\'t b

6条回答
  •  猫巷女王i
    2020-12-21 07:33

    You could use the empty function, which returns true if the variable is either unset or empty (i.e. a zero-length string, 0, NULL, FALSE and so on):

    if(!empty($_POST['variable'])){
        /* ... */
    }
    

    This is pretty much the same test as the one you're doing now, except that it will also return false (not run the block), without warnings, when the variable is unset.

提交回复
热议问题