# Detecting if a null variable exists?

- Thread: https://externals.io/message/48743
- Original archive: https://news-web.php.net/php.internals/48743
- Started by Brian Moon on 2010-06-11T17:43:07+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #48743 — Brian Moon — 2010-06-11T17:43:07+00:00

URL: https://externals.io/message/48743#48743

Is it just me or are we missing a way in the language to check if a 
variable that has been set to `NULL` exists or not?

 `is_null()` on an unset variable throws a NOTICE.
$var === null throws a notice.

So, you have to use isset()? But, ah,

$var = null;
if(isset($var))

yields false.

Is array_key_exists("var", $GLOBALS) the only solution to this problem? 
Seems silly.

perhaps an var_exists() function is needed to fill this hole?
	
-- 

Brian.
--------
http://brian.moonspot.net/

---

## #48744 — Hannes Magnusson — 2010-06-11T18:04:03+00:00

URL: https://externals.io/message/48743#48744 · In reply to: #48743

> Is it just me or are we missing a way in the language to check if a variable
> that has been set to `NULL` exists or not?
>
> `is_null()` on an unset variable throws a NOTICE.
> $var === null throws a notice.
>
> So, you have to use isset()? But, ah,
>
> $var = null;
> if(isset($var))
>
> yields false.
>
> Is array_key_exists("var", $GLOBALS) the only solution to this problem?


This question creeps up every once in a while, and I have seen
bunchload of code that uses array_key_exists("foo", $array); rather
then isset($array["foo"]) because of it.

I however have never seen a proper usecase..
I tend to "initialize" my variables, and array keys, as null, so I
never have to do these kind of checks. And if you don't initialize
your variables, then you can turn of `E_NOTICE` if it bothers you.

-Hannes

---

## #48764 — Michael Wallner — 2010-06-14T06:46:29+00:00

URL: https://externals.io/message/48743#48764 · In reply to: #48743

> Is it just me or are we missing a way in the language to check if a
> variable that has been set to `NULL` exists or not?
>...
> perhaps an var_exists() function is needed to fill this hole?
>

php -r 'function f(){$f=null; var_dump(array_key_exists("f", `get_defined_vars()`));} f();'



Mike

---

## #48771 — Brian Moon — 2010-06-14T11:45:42+00:00

URL: https://externals.io/message/48743#48771 · In reply to: #48764

> php -r 'function f(){$f=null; var_dump(array_key_exists("f",
 > `get_defined_vars()`));} f();'

So, two function calls? That is not a solution to simply check if a 
variable has been created. You are creating an array and operating on 
said array. It should be a language construct. Why use an array function 
to decide something about a non-array variable?


Brian.
--------
http://brian.moonspot.net/

---

## #48773 — Christian Schneider — 2010-06-14T12:08:53+00:00

URL: https://externals.io/message/48743#48773

Frederic Hardy wrote:
> > […]
>
> class foo {
>     public function __call($method, $arguments)
>     {
>         // We MUST have at least ONE argument !
>         if (array_key_exists(0, $arguments) === false) //
> isset($arguments[0]) return false if first argument has null value


Why not simply
	if (count($arguments) == 0)
		..
which is simpler and clearer?

I agree with Hannes that most "usecases" should be solved differently
anyway.

- Chris
