检查变量是否为空 [英] check if variable empty

查看:178
本文介绍了检查变量是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if ($user_id == NULL || $user_name == NULL || $user_logged == NULL) {
    $user_id = '-1';
    $user_name = NULL;
    $user_logged = NULL;
}
if ($user_admin == NULL) {
    $user_admin = NULL;
}




  1. 有没有最短的方法呢?

  2. 如果我没错,应该用 is_null 进行测试?

  3. 可能 $ user_id $ user_name $ user_logged 写一行(也许是数组?)而不重复 NULL

  1. Is there any shortest way to do it ?
  2. And if i right, it should be tested with is_null?
  3. It's possible $user_id, $user_name and $user_logged write in one line (maybe array?) without repeating NULL ?


推荐答案

如果你想测试变量是否 NULL ,请使用身份运算符:

If you want to test whether a variable is really NULL, use the identity operator:

$user_id === NULL  // FALSE == NULL is true, FALSE === NULL is false
is_null($user_id)

如果要检查是否未设置变量:

If you want to check whether a variable is not set:

!isset($user_id)

或者如果变量不为空,空字符串,零,..:

Or if the variable is not empty, an empty string, zero, ..:

empty($user_id)

如果要测试变量是否不是空字符串,也足够了:

If you want to test whether a variable is not an empty string, ! will also be sufficient:

!$user_id

这篇关于检查变量是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆