php 为什么要同时检查 isset() 和 !empty()

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

Why check both isset() and !empty()

php

提问by silow

Is there a difference between issetand !empty. If I do this double boolean check, is it correct this way or redundant? and is there a shorter way to do the same thing?

有没有之间的差异isset!empty。如果我进行双重布尔检查,这种方式是正确的还是多余的?有没有更短的方法来做同样的事情?

isset($vars[1]) AND !empty($vars[1])

回答by deceze

This is completely redundant. emptyis more or less shorthand for !isset($foo) || !$foo, and !emptyis analogous to isset($foo) && $foo. I.e. emptydoes the reverse thing of issetplus an additional check for the truthinessof a value.

这是完全多余的。empty或多或少是 的简写!isset($foo) || !$foo!empty类似于isset($foo) && $foo。即empty做相反的事情,isset加上对值的真实性的额外检查。

Or in other words, emptyis the same as !$foo, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.

或者换句话说,empty与 相同!$foo,但如果变量不存在则不会抛出警告。这就是这个函数的要点:做一个布尔比较而不用担心设置的变量。

The manualputs it like this:

手册是这样写的:

empty()is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

empty()与 相反(boolean) var只是在未设置变量时不生成警告。

You can simply use !empty($vars[1])here.

您可以!empty($vars[1])在这里简单地使用。

回答by GreenMatt

isset()tests if a variable is set and not null:

isset()测试变量是否已设置且不为空:

http://us.php.net/manual/en/function.isset.php

http://us.php.net/manual/en/function.isset.php

empty()can return true when the variable is set to certain values:

empty()当变量设置为某些值时可以返回 true:

http://us.php.net/manual/en/function.empty.php

http://us.php.net/manual/en/function.empty.php

To demonstrate this, try the following code with $the_var unassigned, set to 0, and set to 1.

为了证明这一点,请尝试以下代码,其中 $the_var 未分配,设置为 0,然后设置为 1。

<?php

#$the_var = 0;

if (isset($the_var)) {
  echo "set";
} else {
  echo "not set";
}

echo "\n";

if (empty($the_var)) {
  echo "empty";
} else {
  echo "not empty";
}
?>

回答by Snowcrash

The accepted answer is not correct.

接受的答案不正确。

isset() is NOTequivalent to !empty().

isset()等同于 !empty()。

You will create some rather unpleasant and hard to debug bugs if you go down this route. e.g. try running this code:

如果你走这条路,你会产生一些相当不愉快且难以调试的错误。例如尝试运行此代码:

<?php

$s = '';

print "isset: '" . isset($s) . "'. ";
print "!empty: '" . !empty($s) . "'";

?>

https://3v4l.org/J4nBb

https://3v4l.org/J4nBb

回答by rajmohan

$a = 0;
if (isset($a)) { //$a is set because it has some value ,eg:0
    echo '$a has value';
}
if (!empty($a)) { //$a is empty because it has value 0
    echo '$a is not empty';
} else {
    echo '$a is empty';
}

回答by Breezer

Empty just check is the refered variable/array has an value if you check the php doc(empty)you'll see this things are considered emtpy

Empty 只是检查引用的变量/数组是否有值,如果你检查php doc(empty)你会看到这些东西被认为是 emtpy

* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)
* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)

while isset check if the variable isset and not null which can also be found in the php doc(isset)

while isset 检查变量 isset 和 not null 是否也可以在php doc(isset) 中找到

回答by madlopt

It is not necessary.

这不是必要的。

No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var)|| $var == false.

如果变量不存在,则不会生成警告。这意味着 empty() 本质上是等价于 !isset($var)|| $var == 假。

php.net

php.net

回答by Tajni

isset($vars[1]) AND !empty($vars[1])is equivalent to !empty($vars[1]).

isset($vars[1]) AND !empty($vars[1])相当于!empty($vars[1])

I prepared simple code to show it empirically.

我准备了简单的代码来凭经验展示它。

Output after beautifying. Last row is undefined variable.

美化后输出。最后一行是未定义的变量。

+----------+---------+---------+----------+---------------------+
| Variable | empty() | isset() | !empty() | isset() && !empty() |
+----------+---------+---------+----------+---------------------+
| ''       | true    | true    | false    | false               |
| ' '      | false   | true    | true     | true                |
| false    | true    | true    | false    | false               |
| true     | false   | true    | true     | true                |
| array () | true    | true    | false    | false               |
| NULL     | true    | false   | false    | false               |
| '0'      | true    | true    | false    | false               |
| 0        | true    | true    | false    | false               |
| 0.0      | true    | true    | false    | false               |
| NULL     | true    | false   | false    | false               |
+----------+---------+---------+----------+---------------------+

And code:

和代码:

$var1 = "";
$var2 = " ";
$var3 = FALSE;
$var4 = TRUE;
$var5 = array();
$var6 = null;
$var7 = "0";
$var8 = 0;
$var9 = 0.0;

function compare($var)
{
    print(var_export($var, true) . "|" . 
        var_export(empty($var), true) . "|" . 
        var_export(isset($var), true) . "|" . 
        var_export(!empty($var), true) . "|" . 
        var_export(isset($var) && !empty($var), true) . "\n");
}

for ($i = 1; $i <= 10; $i++) {
    $var = 'var' . $i;
    @compare($$var);
}

回答by Stuart

"Empty": only works on variables. Empty can mean different things for different variable types (check manual: http://php.net/manual/en/function.empty.php).

“Empty”:仅适用于变量。空对于不同的变量类型可能意味着不同的东西(检查手册:http: //php.net/manual/en/function.empty.php)。

"isset": checks if the variable exists and checks for a true NULL or false value. Can be unset by calling "unset". Once again, check the manual.

“isset”:检查变量是否存在并检查是否为真 NULL 或假值。可以通过调用“unset”来取消设置。再次检查手册。

Use of either one depends of the variable type you are using.

使用任何一种取决于您使用的变量类型。

I would say, it's safer to check for both, because you are checking first of all if the variable exists, and if it isn't really NULL or empty.

我会说,同时检查两者更安全,因为您首先检查变量是否存在,以及它是否不是真正的 NULL 或空。

回答by diEcho

if we use same page to add/edit via submit button like below

如果我们使用相同的页面通过如下提交按钮添加/编辑

<input type="hidden" value="<?echo $_GET['edit_id'];?>" name="edit_id">

then we should not use

那么我们不应该使用

isset($_POST['edit_id'])

bcoz edit_idis set all the time whether it is add or edit page , instead we should use check below condition

edit_id无论是添加页面还是编辑页面,bcoz 一直被设置,而我们应该使用下面的条件检查

!empty($_POST['edit_id'])

回答by Ende

  • From the PHP Web site, referring to the empty()function:
  • 来自PHP网站,指的是empty()函数:

Returns FALSEif varhas a non-empty and non-zero value.

返回FALSE如果var有一个非空和非零值。

That's a good thing to know. In other words, everything from NULL, to 0to “” will return TRUEwhen using the empty()function.

知道这一点是件好事。换句话说,从NULL, 到0“” 的所有内容都将TRUE在使用该empty()函数时返回。

  • Here is the description of what the isset()function returns:
  • 以下是isset()函数返回内容的描述:

Returns TRUEif varexists; FALSEotherwise.

TRUE如果var存在则返回;FALSE除此以外。

In other words, only variables that don't exist (or, variables with strictly NULLvalues) will return FALSEon the isset()function. All variables that have any type of value, whether it is 0, a blank text string, etc. will return TRUE.

换句话说,只有不存在的变量(或具有严格NULL值的变量)才会FALSEisset()函数上返回。所有具有任何类型值的变量,无论是0、空白文本字符串等,都将返回TRUE