PHP 检查是否为 False 或 Null

2022-08-30 18:28:25

我也感到困惑,如何检查变量是否/当从函数返回时。falsenull

何时使用和何时使用检查条件?empty()isset()


答案 1

对于函数的返回,您既不使用也不使用 或 ,因为它们只对变量起作用,并且只是为了测试可能不存在的变量而不会触发错误。issetempty

对于函数返回,检查变量的存在是毫无意义的,所以只需这样做:

if (!my_function()) {
    // function returned a falsey value
}

要更详细地阅读有关此内容的信息,请参阅 PHP 的权威指南 isset and empty


答案 2

检查变量 (几个例子 )

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)

推荐