JavaScript do 'estrito não igual' operador ( !==) em comparação com os undefinedque não resultar em falseem nullvalores.
var createTouch = null;
isTouch = createTouch !== undefined
Para obter um comportamento equivalente em PHP, você pode verificar se o nome da variável existe nas chaves do resultado de 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();
Na maioria dos casos, isset($variable)é apropriado. Isso é aquivalente para array_key_exists('variable', get_defined_vars()) && null !== $variable. Se você apenas usar null !== $variablesem pré-verificar a existência, irá bagunçar seus logs com avisos porque isso é uma tentativa de ler o valor de uma variável indefinida.
No entanto, você pode aplicar uma variável indefinida a uma referência sem qualquer aviso:
function my_isset(&$var)
{
return null === $var;
}
$is_set = my_isset($undefined_variable);