JavaScript dell'operatore 'rigorosa non uguale' ( !==) sul confronto con undefinednon non comporta falsesu nullvalori.
var createTouch = null;
isTouch = createTouch !== undefined
Per ottenere un comportamento equivalente in PHP, puoi controllare se il nome della variabile esiste nelle chiavi del risultato di get_defined_vars().
const BR = '<br>' . PHP_EOL;
$test = 1;
function test()
{
echo '$test ' . ( array_key_exists('test', get_defined_vars())
? 'exists.' : 'does not exist.' ) . BR;
$test = null;
echo '$test ' . ( array_key_exists('test', get_defined_vars())
? 'exists.' : 'does not exist.' ) . BR;
$test = 1;
echo '$test ' . ( array_key_exists('test', get_defined_vars())
? 'exists.' : 'does not exist.' ) . BR;
unset($test);
echo '$test ' . ( array_key_exists('test', get_defined_vars())
? 'exists.' : 'does not exist.') . BR;
}
test();
Nella maggior parte dei casi, isset($variable)è appropriato. Questo è equivalente a array_key_exists('variable', get_defined_vars()) && null !== $variable. Se usi solo null !== $variablesenza precontrollare l'esistenza, incasinerai i tuoi log con avvisi perché è un tentativo di leggere il valore di una variabile non definita.
Tuttavia, puoi applicare una variabile non definita a un riferimento senza alcun avviso:
function my_isset(&$var)
{
return null === $var;
}
$is_set = my_isset($undefined_variable);