D
D
Donald_Duck2017-10-04 12:09:52
PHP
Donald_Duck, 2017-10-04 12:09:52

How to distinguish a variable declared as null from an undeclared variable?

How to distinguish a variable declared as null from an undeclared variable?

$a = null;

var_dump(isset($a)); // false
var_dump(isset($a)); // false

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2017-10-04
@Donald_Duck

php.net/manual/en/function.is-null.php

<?php
error_reporting(E_ALL); 

set_error_handler('eh');
function eh($severity, $message, $filename, $lineno) {
  if (error_reporting() == 0) {
    return;
  }
  if (error_reporting() & $severity) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
  }
}

//$abc=NULL;

try {
  if($abc===NULL) echo 'NULL';
} catch (Exception $e) {
  echo 'NOT DEFINED';
}

?>

Another option:
<?php
error_reporting(E_ALL); 

//$abc=NULL;

if(array_key_exists('abc',get_defined_vars())) var_dump($abc);
else echo 'NOT DEFINED';

?>

D
Don Gan, 2017-10-04
@PravdorubMSK

isset

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question