PHP: How to check if var initiliazed was? isset returns false if var is set to NULL

<?php
    $var = NULL;

    var_dump(isset($var)); // bool(false)
    var_dump(isset($unset_var)); // bool(false)
?>

isset ($ var) should return TRUE since it was set to NULL.

Is there any way to check this?

Thanks!

+2
source share
6 answers

use get_defined_vars () to get an array of variables defined in the current scope and then test against it with array_key_exists ();

Edited by:

if you want the function to check for existence, you must create the same:

function varDefined($name,$scope) {
  return array_key_exists($name, $scope);
}

and use them like this in any given area:

$exists = varDefined('foo',get_defined_vars());

Should work in any field.

+7
source

Not very pretty, but ...

array_key_exists('var', $GLOBALS);

( @is_null($var), true [ @...))

+1

, , $GLOBALS ( array_key_exists()).

, , , :)

0

, :

if(array_key_exists('var', $GLOBALS))
0

unset var null:

php > $a = null;
php > var_dump($a === null);
bool(true)
php > var_dump($a === $b);
bool(true)

( - php -a)

-1

NULL? , , , , NULL.

-1

Source: https://habr.com/ru/post/1702835/


All Articles