PHP 中的“isset()”和“!empty()”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20582962/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:18:57  来源:igfitidea点击:

What's the difference between 'isset()' and '!empty()' in PHP?

phpisset

提问by Vitalynx

I don't understand the difference between isset()and !empty().

我不明白之间的差别isset()!empty()

Because if a variable has been set, isn't it the same as not being empty?

因为如果一个变量已经被设置了,这不是和不为空一样吗?

回答by Nambi

ISSETchecks the variable to see if it has been set. In other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSETreturns TRUEif the variable exists and has a value other than NULL. That means variables assigned a "", 0, "0", or FALSE are set, and therefore are TRUEfor ISSET.

ISSET检查变量以查看它是否已设置。换句话说,它检查变量是否是除 NULL 以外的任何值或未分配值。如果变量存在并且具有非 NULL 值,则ISSET返回TRUE。这意味着设置了分配了“”、0、“0”或 FALSE 的变量,因此适用TRUEISSET

EMPTYchecks to see if a variable is empty. Empty is interpreted as: "" (an empty string), 0 (integer), 0.0 (float)`, "0" (string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class.

EMPTY检查变量是否为空。Empty 被解释为:“”(空字符串)、0(整数)、0.0(浮点数)`、“0”(字符串)、NULL、FALSE、array()(空数组)和“$var;” (声明的变量,但在类中没有值。

For more information, see this article

有关更多信息,请参阅这篇文章

回答by Black Mamba

Source :http://php.net/manual/en/types.comparisons.phpThis page shows the comparison of the empty(),is_null(),isset().

来源:http://php.net/manual/en/types.comparisons.php本页展示了empty(), is_null(),的比较 isset()

The picture showing complete comparison here

此处显示完整比较的图片

回答by Prashant16

The type comparison tables gives answer of all question about these operators

类型比较表给出了有关这些运算符的所有问题的答案

http://php.net/manual/en/types.comparisons.php

http://php.net/manual/en/types.comparisons.php

回答by joy

Isset return false if variable has not been set or it is null and return true if variable has been set and not null.

如果变量尚未设置或为空,则 Isset 返回 false;如果变量已设置且不为空,则返回 true。

!empty return true if variable has been set and not empty. Empty string, empty array, "0",0 and false are defined as empty.

!empty 如果变量已设置且不为空,则返回 true。空字符串、空数组、“0”、0 和 false 定义为空。

回答by user3102083

isset— Determine if a variable is set and is not NULL.

isset— 确定变量是否已设置且不为 NULL。

!empty— Determine whether a variable is NOT empty.

!empty— 确定变量是否非空。

回答by Andrey P.

And one more remark. empty()checks if the variable exists as well. I.e. if we perform empty()to the variable that wasn't declared, we don't receive an error, empty()returns 'true'. Therefore we may avoid isset()if next we need to check if the variable empty.

还有一句话。empty()检查变量是否也存在。即,如果我们empty()对未声明的变量执行,我们不会收到错误,empty()返回“真”。因此,isset()如果接下来我们需要检查变量是否为空,我们可以避免。

So

所以

isset($var) && !empty($var)

will be equals to

将等于

!empty($var)