$a = NULL;
$c = 1;
var_dump(isset($a)); // bool(false)
var_dump(isset($b)); // bool(false)
var_dump(isset($c)); // bool(true)
Run Code Online (Sandbox Code Playgroud)
我如何从"真正不存在"中区分出$a存在但具有价值的东西?NULL$b
使用以下内容:
$a = NULL;
var_dump(true === array_key_exists('a', get_defined_vars()));
Run Code Online (Sandbox Code Playgroud)
知道为什么要这样做会很有趣,但无论如何,这是可能的:
使用get_defined_vars,它将包含当前范围内已定义变量的条目,包括具有 NULL 值的变量。这是它的使用示例
function test()
{
$a=1;
$b=null;
//what is defined in the current scope?
$defined= get_defined_vars();
//take a look...
var_dump($defined);
//here's how you could test for $b
$is_b_defined = array_key_exists('b', $defined);
}
test();
Run Code Online (Sandbox Code Playgroud)
这显示
array(2) {
["a"] => int(1)
["b"] => NULL
}
Run Code Online (Sandbox Code Playgroud)