检查变量是否为空 [英] check if variable empty
本文介绍了检查变量是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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;
}
- 有没有最短的方法呢?
- 如果我没错,应该用
is_null
进行测试? - 可能
$ user_id
,$ user_name
和$ user_logged
写一行(也许是数组?)而不重复NULL
?
- Is there any shortest way to do it ?
- And if i right, it should be tested with
is_null
? - It's possible
$user_id
,$user_name
and$user_logged
write in one line (maybe array?) without repeatingNULL
?
推荐答案
如果你想测试变量是否 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屋!
查看全文