PHP 5.4 - 由字符串定义的变量,它的值设置为NULL未定义

Can anyone tell my why the variable defined by string does not exists?

$string = 'variable';
${$string} = NULL;

echo $variable;

Variable $variable is not defined.

According to the documentation

isset — Determine if a variable is set and is not NULL

Since you set the variable to NULL like

$string = 'variable';
${$string} = NULL;

it will return false

An undefined variable in PHP has value NULL, therefore:

${$string} == $variable;
${$string2} == $variable2;

${$string2} == $variable2 == 'value'; // so isset returns TRUE
$variable == NULL; // so isset returns FALSE
${$string} == $variable == NULL; // so isset returns FALSE

As pointed by others, isset() checks if the variable is set and is not null.

If you want to check if the variable is defined at all including the ones set as null, you can use get_defined_vars() to get list defined variables and check if the variable name is there:

$string = null;
$string2 = '';

var_dump(array_key_exists('string', get_defined_vars())); // bool(true)
var_dump(array_key_exists('string2', get_defined_vars())); // bool(true)
var_dump(array_key_exists('string3', get_defined_vars())); // bool(false)