check if variable empty

前端 未结 12 1353
旧时难觅i
旧时难觅i 2020-11-27 14:00
if ($user_id == NULL || $user_name == NULL || $user_logged == NULL) {
    $user_id = \'-1\';
    $user_name = NULL;
    $user_logged = NULL;
}
if ($user_admin == NUL         


        
12条回答
  •  旧时难觅i
    2020-11-27 14:23

    Its worth noting - and I only found this out after nearly 9 years of PHP coding that the best way of checking any variable exists is using the empty() function. This is because it doesn't generate errors and even though you turn them off - PHP still generates them! empty() however won't return errors if the variable doesn't exist. So I believe the correct answer is not to check if its null but to do the following

    if (!empty($var) && is_null($var))
    

    Note the PHP manual

    variable is considered empty if it does not exist or if its value equals FALSE

    As opposed to being null which is handy here!

提交回复
热议问题