PHP:变量为空或未设置或什么?

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

PHP: Variable empty or not set or what?

phpvariablesconditional

提问by caw

What is the difference between these four PHP statements?

这四个 PHP 语句有什么区别?

if (isset($data)) {

if (!empty($data)) {

if ($data != '') {

if ($data) {

Do they all do the same?

他们都做同样的事情吗?

回答by jlb

Check PHP manual out: http://www.php.net/manual/en/types.comparisons.php

查看 PHP 手册:http: //www.php.net/manual/en/types.comparisons.php

Expression  gettype() empty() is_null() isset() if($x)
$x = "";        string  TRUE    FALSE   TRUE    FALSE
$x = null;      NULL    TRUE    TRUE    FALSE   FALSE
var $x;         NULL    TRUE    TRUE    FALSE   FALSE
$x undefined    NULL    TRUE    TRUE    FALSE   FALSE
$x = array();   array   TRUE    FALSE   TRUE    FALSE
$x = false;     boolean TRUE    FALSE   TRUE    FALSE
$x = true;      boolean FALSE   FALSE   TRUE    TRUE
$x = 1;         integer FALSE   FALSE   TRUE    TRUE
$x = 42;        integer FALSE   FALSE   TRUE    TRUE
$x = 0;         integer TRUE    FALSE   TRUE    FALSE
$x = -1;        integer FALSE   FALSE   TRUE    TRUE
$x = "1";       string  FALSE   FALSE   TRUE    TRUE
$x = "0";       string  TRUE    FALSE   TRUE    FALSE
$x = "-1";      string  FALSE   FALSE   TRUE    TRUE
$x = "php";     string  FALSE   FALSE   TRUE    TRUE
$x = "true";    string  FALSE   FALSE   TRUE    TRUE
$x = "false";   string  FALSE   FALSE   TRUE    TRUE

As you can see, if(!empty($x))is equal to if($x)and if(!is_null($x))is equal to if(isset($x)). As far as if $data != ''goes, it is TRUEif $datais not NULL, '', FALSEor 0(loose comparison).

如您所见,if(!empty($x))等于if($x)并且if(!is_null($x))等于if(isset($x))。就 if$data != ''而言,它是TRUEif$data不是NULL''FALSE0(松散比较)。

回答by hsz

if (isset($data)) {  

Variable is just set - before that line we declared new variable with name 'data', i.e. $data = 'abc';

变量刚刚设置 - 在该行之前我们声明了名为“data”的新变量,即 $data = 'abc';

if (!empty($data)) {  

Variable is filled with data. It cannot have empty array because then $datahas array type but still has no data, i.e. $data = array(1); Cannot be null, empty string, empty array, empty object, 0, etc.

变量填充了数据。它不能有空数组,因为它$data有数组类型但仍然没有数据,即 $data = array(1); 不能为空、空字符串、空数组、空对象、0 等。

if ($data != '') {  

Variable is not an empty string. But also cannot be empty value (examples above).
If we want to compare types, use !==or ===.

变量不是空字符串。但也不能是空值(上面的例子)。
如果我们想比较类型,请使用!=====

if ($data) {  

Variable is filled out with any data. Same thing as !empty($data).

变量用任何数据填充。一样的东西!empty($data)

回答by meder omuraliev

They aren't the same.

他们不一样。

  1. true if the variable is set. the variable can be set to blank and this would be true.

  2. true if the variable is set anddoes not equal empty string, 0, '0', NULL, FALSE, blank array. it is clearly not the same as isset.

  3. if the variable does not equal an empty string, if the variable isnt set its an empty string.

  4. if the variable coerces to true, if the variable isnt set it will coerce to false.

  1. 如果设置了变量,则为 true。该变量可以设置为空白,这将是正确的。

  2. 如果该变量被设置为真不等于空字符串,0,“0”,NULL,FALSE,空白阵列。它显然与isset.

  3. 如果变量不等于空字符串,如果变量没有设置为空字符串。

  4. 如果变量强制为真,如果变量未设置,它将强制为假。

回答by Skawful

if (isset($data)) - Determines if a variable is set (has not bet 'unset()'and is not NULL.

if (isset($data)) - 确定是否设置了一个变量(没有下注'unset()'并且不是NULL.

if (!empty($data)) - Is a type agnostic check for empty if $data was '', 0, false, or NULL it would return true.

if (!empty($data)) - 如果 $data 为 ''、0、false 或 NULL,则是一种类型不可知的空检查,它将返回 true。

if ($data != '') { this is a string type safe of checking whether $data is not equal to an empty string

if ($data != '') { 这是检查 $data 是否不等于空字符串的字符串类型安全

if ($data) { this is a looking for true or false (aka: 0 or 1)

if ($data) { 这是在寻找真假(又名:0 或 1)