PHP 检查是否为 False 或 Null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11464714/
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 check if False or Null
提问by Harsha M V
I also get confused how to check if a variable is false/null when returned from a function.
我也对如何在从函数返回时检查变量是否为 false/null 感到困惑。
When to use empty() and when to use isset() to check the condition ?
何时使用 empty() 以及何时使用isset() 检查条件?
采纳答案by deceze
For returns from functions, you use neither issetnor empty, since those only work on variables and are simply there to test for possibly non-existing variables without triggering errors.
对于函数的返回值,您既不使用isset也不使用empty,因为它们只对变量起作用,并且只是为了测试可能不存在的变量而不会触发错误。
For function returns checking for the existence of variables is pointless, so just do:
对于函数返回检查变量的存在是没有意义的,所以只需执行以下操作:
if (!my_function()) {
// function returned a falsey value
}
To read about this in more detail, see The Definitive Guide To PHP's isset And empty.
要更详细地了解这一点,请参阅PHP 的 isset 和 empty 权威指南。
回答by Arkadiusz 'flies' Rzadkowolski
Checking variable ( a few examples )
检查变量(几个例子)
if(is_null($x) === true) // null
if($x === null) // null
if($x === false)
if(isset($x) === false) // variable undefined or null
if(empty($x) === true) // check if variable is empty (length of 0)
回答by Rupesh Pawar
Isset() checks if a variable has a value including ( False , 0 , or Empty string) , But not NULL. Returns TRUE if var exists; FALSE otherwise.
Isset() 检查变量的值是否包括 ( False 、 0 或 Empty string) ,但不包括 NULL。如果 var 存在,则返回 TRUE;否则为假。
On the other hand the empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.
另一方面,empty() 函数检查变量是否具有空值空字符串、0、NULL 或 False。如果 var 具有非空和非零值,则返回 FALSE。
回答by Tufan Bar?? Y?ld?r?m
ISSETchecks the variable to see if it has been set, in other words, it checks to see if the variable is any value except NULLor not assigned a value. ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a " ", 0, "0", or FALSE are set, and therefore are TRUE for ISSET.
ISSET检查变量以查看它是否已设置,换句话说,它检查变量是否为除NULLor之外的任何值not assigned a value。如果变量存在且具有非 NULL 值,则 ISSET 返回 TRUE。这意味着分配了“ ”、0、“0”或 FALSE 的变量已设置,因此对于 ISSET 为 TRUE。
EMPTYchecks to see if a variable is empty. Empty is interpreted as: " " (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), and "$var;" (a variable declared, but without a value in a class.
EMPTY检查变量是否为空。Empty 被解释为:“”(空字符串)、0(0 作为整数)、0.0(0 作为浮点数)、“0”(0 作为字符串)、NULL、FALSE、array()(空数组), 和 "$var;" (声明的变量,但在类中没有值。
回答by Naren Karthik
isset — Determine if a variable is set and is not NULL
isset — 确定变量是否已设置且不为 NULL
$a = "test";
$b = "anothertest";
var_dump(isset($a)); // TRUE
var_dump(isset($a, $b)); // TRUE
unset ($a);
var_dump(isset($a)); // FALSE
empty — Determine whether a variable is empty
empty - 确定变量是否为空
<?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';
}
?>
回答by Allen
- check false: if ($v === false)
- check null: if (is_null($v))
- 检查错误:如果($v === false)
- 检查空值:if (is_null($v))
empty() is an evil.It is slow,and when $v queals false,0,'0',array(),'',it will return true.if you need this kind of checking,you can use if ($v).
empty() 是一个邪恶。它很慢,当 $v 查询false,0,'0',array(),'' 时,它会返回 true。如果你需要这种检查,你可以使用if ($五)。

