php php是空的还是空的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8236354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php is null or empty?
提问by Erin Tucker
I have a question regarding NULL
in PHP:
我有一个关于NULL
PHP的问题:
$a = '';
if($a == NULL) {
echo 'is null';
}
Why do I see is nullwhen $a
is an empty string? Is that a bug?
为什么我看到的是$a
空字符串时为空?这是一个错误吗?
回答by Godwin
What you're looking for is:
你要找的是:
if($variable === NULL) {...}
Note the ===
.
When use ==
, as you did, PHP treats NULL, false, 0, the emptystring, and emptyarrays as equal.
请注意===
.
使用时==
,正如您所做的那样,PHP 将NULL、false、0、空字符串和空数组视为相等。
回答by PHPst
As is shown in the following table, empty($foo)
is equivalent to $foo==null
and is_null($foo)
has the same function of $foo===null
. The table also shows some tricky values regarding the null
comparison. (? denotes an uninitialized variables. )
如下表所示,empty($foo)
相当于$foo==null
并is_null($foo)
具有相同的功能$foo===null
。该表还显示了一些有关null
比较的棘手值。(? 表示一个未初始化的变量。)
empty is_null
==null ===null isset array_key_exists
? | T | T | F | F
null | T | T | F | T
"" | T | F | T | T
[] | T | F | T | T
0 | T | F | T | T
false | T | F | T | T
true | F | F | T | T
1 | F | F | T | T
TRUE FALSE 1 0 -1 "1" "0" "-1" NULL array() "php" ""
[...]
"" FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
| F | F | T | T
I never use empty()
and is_null()
functions. Using simple comparison is less ambiguous, faster and cleaner.Particularly there will be less curly brackets to match.
我从不使用empty()
和is_null()
函数。使用简单的比较可以减少歧义、更快、更清晰。特别是匹配的大括号会更少。
e.g if($x==null || $y==null)
vs if(is_null($x) || is_null($y))
例如if($x==null || $y==null)
vsif(is_null($x) || is_null($y))
回答by jancha
check ==
vs ===
检查==
与===
'' == NULL
would return true0 == NULL
would return truefalse == null
would return true
'' == NULL
会返回true0 == NULL
会返回truefalse == null
会返回true
where as
然而
'' === NULL
would return false0 === NULL
would return falsefalse === NULL
would return false
'' === NULL
会返回假0 === NULL
会返回假false === NULL
会返回假
回答by Felix Kling
No it's not a bug. Have a look at the Loose comparisons with ==table(second table), which shows the result of comparing each value in the first column with the values in the other columns:
不,这不是错误。查看与 ==表(第二个表)的松散比较,它显示了将第一列中的每个值与其他列中的值进行比较的结果:
'' == null == 0 == false
There you can see that an empty string ""
compared with false
, 0
, NULL
or ""
will yield true.
在那里,你可以看到一个空字符串""
相比false
,0
,NULL
或""
将产生真实的。
You might want to use is_null
[docs]instead, or strict comparison (third table).
您可能想改用is_null
[docs]或严格比较(第三个表)。
回答by Aurelio De Rosa
This is nota bug but PHP normal behavior. It happens because the ==
operator in PHP doesn't check for type.
这不是错误而是 PHP 的正常行为。这是因为==
PHP 中的运算符不检查类型。
$a = '';
if(empty($a)) {
echo 'is empty';
}
If you want also to check if the values have the same type, use ===
instead. To study in deep this difference, please read the official documentation.
如果您还想检查值是否具有相同的类型,请===
改用。要深入研究这种差异,请阅读官方文档。
回答by dayuloli
If you use ==
, php treats an empty string or array as null
. To make the distinction between null
and empty
, either use ===
or is_null
. So:
如果使用==
,php 会将空字符串或数组视为null
。要区分null
和empty
,请使用===
或is_null
。所以:
if($a === NULL)
or if(is_null($a))
if($a === NULL)
或者 if(is_null($a))
回答by ewizard
Use empty
- http://php.net/manual/en/function.empty.php.
使用empty
- http://php.net/manual/en/function.empty.php。
Example:
例子:
##代码##