php isset() 和 empty() - 使用什么

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

isset() and empty() - what to use

php

提问by Dmitry Belaventsev

Could you help me to improve my coding style?:) In some tasks I need to check - is variable empty or contains something. To solve this task, I usually do the following.

你能帮我改进我的编码风格吗?:) 在我需要检查的一些任务中 - 变量是空的还是包含一些东西。为了解决这个任务,我通常会做以下事情。

Check - is this variable set or not? If it's set - I check - it's empty or not?

检查 - 这个变量是否设置?如果它已设置 - 我检查 - 它是空的还是不?

<?php
    $var = '23';
    if (isset($var)&&!empty($var)){
        echo 'not empty';
    }else{
        echo 'is not set or empty';
    }
?>

And I have a question - should I use isset() before empty() - is it necessary? TIA!

我有一个问题 - 我应该在 empty() 之前使用 isset() - 有必要吗?蒂亚!

回答by Pez Cuckow

It depends what you are looking for, if you are just looking to see if it is empty just use emptyas it checks whether it is set as well, if you want to know whether something is set or not use isset.

这取决于您要查找的内容,如果您只是想查看它是否为空,请使用empty它,因为它会检查它是否也已设置,如果您想知道是否设置了某些内容,请使用isset

Emptychecks if the variable is set and if it is it checks it for null, "", 0, etc

Empty检查变量是否已设置,如果是,则检查它是否为空、""、0 等

Issetjust checks if is it set, it could be anything not null

Isset只是检查它是否设置,它可能不是空的

With empty, the following things are considered empty:

使用empty,以下内容被视为空:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)
  • ""(空字符串)
  • 0(0 作为整数)
  • 0.0(0 作为浮点数)
  • “0”(0 作为字符串)
  • 空值
  • 错误的
  • array()(空数组)
  • var $var; (声明的变量,但在类中没有值)

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

来自http://php.net/manual/en/function.empty.php



As mentioned in the comments the lack of warning is also important with empty()

正如评论中提到的,没有警告对于 empty() 也很重要

PHP Manualsays

PHP手册

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

empty() 与 (boolean) var 相反,除了在未设置变量时不会生成警告

Regarding isset

关于 isset

PHP Manualsays

PHP手册

isset() will return FALSE if testing a variable that has been set to NULL

如果测试已设置为 NULL 的变量,isset() 将返回 FALSE



Your code would be fine as:

你的代码会很好,因为:

<?php
    $var = '23';
    if (!empty($var)){
        echo 'not empty';
    }else{
        echo 'is not set or empty';
    }
?>


For example:

例如:

$var = "";

if(empty($var)) // true because "" is considered empty
 {...}
if(isset($var)) //true because var is set 
 {...}

if(empty($otherVar)) //true because $otherVar is null
 {...}
if(isset($otherVar)) //false because $otherVar is not set 
 {...}

回答by deceze

In your particular case: if ($var).

你的具体情况:if ($var)

You need to use issetif you don't know whether the variable existsor not. Since you declared it on the very first line though, you know it exists, hence you don't need to, nay, should notuse isset.

isset如果您不知道变量是否存在,则需要使用。由于您在第一行声明了它,因此您知道它存在,因此您不需要,不,不应该使用isset.

The same goes for empty, only that emptyalso combines a check for the truthinessof the value. emptyis equivalent to !isset($var) || !$varand !emptyis equivalent to isset($var) && $var, or isset($var) && $var == true.

也是如此empty,只是empty它还结合了对值真实性的检查。empty等价于!isset($var) || !$var!empty等价于isset($var) && $var, 或isset($var) && $var == true

If you only want to test a variable that should existfor truthiness, if ($var)is perfectly adequate and to the point.

如果您只想测试一个应该真实性而存在的变量,if ($var)则完全足够并且切中要害

回答by chris

You can just use empty() - as seen in the documentation, it will return false if the variable has no value.

您可以只使用 empty() - 如文档中所示,如果变量没有值,它将返回 false。

An example on that same page:

同一页面上的一个例子:

<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>

You can use isset if you just want to know if it is not NULL. Otherwise it seems empty() is just fine to use alone.

如果您只想知道它是否为 NULL,则可以使用 isset。否则似乎 empty() 单独使用就可以了。

回答by Whiskas

Empty returns true if the var is not set. But isset returns true even if the var is not empty.

如果未设置 var,则 Empty 返回 true。但是即使 var 不为空,isset 也会返回 true。

回答by dnagirl

Here are the outputs of isset()and empty()for the 4 possibilities: undeclared, null, false and true.

以下是4 种可能性的isset()和输出empty():未声明、空、假和真。

$a=null;
$b=false;
$c=true;

var_dump(array(isset($z1),isset($a),isset($b),isset($c)),true); //$z1 previously undeclared
var_dump(array(empty($z2),empty($a),empty($b),empty($c)),true); //$z2 previously undeclared

//array(4) { [0]=> bool(false) [1]=> bool(false) [2]=> bool(true) [3]=> bool(true) } 
//array(4) { [0]=> bool(true) [1]=> bool(true) [2]=> bool(true) [3]=> bool(false) } 

You'll notice that all the 'isset' results are opposite of the 'empty' results exceptfor case $b=false. All the values (except null which isn't a value but a non-value) that evaluate to false will return true when tested for by issetand false when tested by 'empty'.

您会注意到,除了case之外,所有 'isset' 结果都与 'empty' 结果相反$b=false。所有评估为 false 的值(除了不是值而是非值的 null)在被测试时将返回真,isset当被“空”测试时将返回假。

So use isset()when you're concerned about the existence of a variable. And use emptywhen you're testing for true or false. If the actual type of emptiness matters, use is_nulland ===0, ===false, ===''.

因此,isset()当您担心变量的存在时使用。并empty在您测试真假时使用。如果空的实际类型很重要,请使用is_nulland ===0, ===false, ===''

回答by Akos

$var = 'abcdef';
if(isset($var))
{
   if (strlen($var) > 0);
   {
      //do something, string length greater than zero
   }
   else
   {
     //do something else, string length 0 or less
   }
}

This is a simple example. Hope it helps.

这是一个简单的例子。希望能帮助到你。

edit: added issetin the event a variable isn't defined like above, it would cause an error, checking to see if its first set at the least will help remove some headache down the road.

编辑:isset如果变量没有像上面那样定义,则添加,它会导致错误,检查它的第一组是否至少有助于消除一些麻烦。