# variable_exists() patch

- Thread: https://externals.io/message/3892
- Original archive: https://news-web.php.net/php.internals/3892
- Started by Lars Torben Wilson on 2003-08-13T22:25:41+00:00
- Messages: 25
- 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

---

## #3892 — Lars Torben Wilson — 2003-08-13T22:25:41+00:00

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

Hi out there,

A few weeks ago I submitted a patch in the bug db for a 
variable_exists() construct, which parallels the `function_exists()`
one but for variables. In short, it returns `TRUE` if a variable
exists, regardless of its value. In other words, it's an isset() 
which doesn't care if the variable's value is NULL.

The bug report and a link to the patch is here:

   http://bugs.php.net/bug.php?id=24274&edit=1

Just mentioning it because I don't know whether it's good enough to
go in, but users have emailed me to ask why it hasn't been included.
If I've screwed up with the patch, a quick note as to what was wrong
would be great.


Thanks,

Torben


-- 
 Torben Wilson <torben@php.net>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====

---

## #3937 — Andi Gutmans — 2003-08-14T15:42:13+00:00

URL: https://externals.io/message/3892#3937 · In reply to: #3892

I am not really convinced either that variable_exists() is 
 `function_exists()` parallel.
Under what circumstances is this needed?

Andi

At 03:18 PM 8/14/2003 +0200, nicos@php.net wrote:

>"Lars Torben Wilson" <torben@php.net> a écrit dans le message de
>news:1060813541.901.336.camel@ali...
> > […]
>
>Hmm, I thought a variable is set even if its value is NULL?
>
>Are you sure we want this patch?
>
> > […]
>
>--
>Regards.
>M.CHAILLAN Nicolas
>nicos@php.net
>www.WorldAKT.com Hébergement de sites internets.

---

## #3936 — Ilia Alshanetsky — 2003-08-14T15:45:38+00:00

URL: https://externals.io/message/3892#3936 · In reply to: #3892

Do we really need this function? I see 2 ways of 'implementing' this 
functionality in PHP without having to add another function. For example:
(isset($var) || is_null($var)) or gettype($var).

Ilia


> Hi out there,
>
> A few weeks ago I submitted a patch in the bug db for a
> variable_exists() construct, which parallels the `function_exists()`
> one but for variables. In short, it returns `TRUE` if a variable
> exists, regardless of its value. In other words, it's an isset()
> which doesn't care if the variable's value is NULL.
>
> The bug report and a link to the patch is here:
>
>    http://bugs.php.net/bug.php?id=24274&edit=1
>
> Just mentioning it because I don't know whether it's good enough to
> go in, but users have emailed me to ask why it hasn't been included.
> If I've screwed up with the patch, a quick note as to what was wrong
> would be great.
>
>
> Thanks,
>
> Torben
>
>
> --
>  Torben Wilson <torben@php.net>                        +1.604.709.0506
>  http://www.thebuttlesschaps.com          http://www.inflatableeye.com
>  http://www.hybrid17.com                  http://www.themainonmain.com
>  -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====

---

## #3946 — walt boring — 2003-08-14T18:15:51+00:00

URL: https://externals.io/message/3892#3946 · In reply to: #3936

Ilia Alshanetsky wrote:

>Do we really need this function? I see 2 ways of 'implementing' this 
>functionality in PHP without having to add another function. For example:
>(isset($var) || is_null($var)) or gettype($var).
>
>Ilia
>  
>
I for one would like to see something like variable_exists(), as I am 
very annoyed with
the logic of isset() returning false if the variable exists and has a 
value of null. 

  The name of the function in this case doesn't not fit the logic for 
isset().  The variable does exist and it IS in fact set, the value just 
so happens to be null.
I constantly get puzzled looks and confusion, when I explain to folks 
that are new to php, that isset() really doesn't do what it's name 
suggests. 

  I for one would much rather do
  if ( variable_exists($var) ) {}  versus
if (isset($var) || is_null($var)) {}

it's less code to type and is less prone to errors.

So I would like to see the patch in, or something similar in 
functionality, since isset() doesn't do what I'd expect.
Walt

---

## #3947 — Stefan Walk — 2003-08-14T18:28:06+00:00

URL: https://externals.io/message/3892#3947 · In reply to: #3946

> I for one would like to see something like variable_exists(), as I am 
> very annoyed with
> the logic of isset() returning false if the variable exists and has a 
> value of null. 
[snip] 
>  I for one would much rather do
>  if ( variable_exists($var) ) {}  versus
> if (isset($var) || is_null($var)) {}

When do you need to do that? I can't think of many situations where it
would be neccessary to check if a variable really exists.
-- 
Regards,
Stefan Walk
<swalk@prp.physik.tu-darmstadt.de

---

## #3948 — Timm Friebe — 2003-08-14T18:43:31+00:00

URL: https://externals.io/message/3892#3948

[...]
> Hmm, I thought a variable is set even if its value is NULL?

Unfortunately it is not:

thekid@friebes:~ > php -r '$a= NULL; var_dump(isset($a));'
bool(false)

- Timm

---

## #3949 — walt boring — 2003-08-14T19:00:26+00:00

URL: https://externals.io/message/3892#3949 · In reply to: #3947

Stefan Walk wrote:

>
>  
>
> > […]
>[snip] 
>  
>
> > […]
>
>When do you need to do that? I can't think of many situations where it
>would be neccessary to check if a variable really exists.
>  
>
It can happen quite easily.  I always develop with full warnings/errors on.
So if for example a var isn't set for whatever reason,  then trying to 
access the
variable will throw a php Notice.  variable_exists() would prevent that, 
as does isset().
isset() would work for my example below, but it still is a 'broken' 
function in my opinion.

if ($var) {  // <-- you'll get a php notice on this line
  switch ($var) {
    ...
  }

}

versus doing

if (variable_exists($var))  {  //no php notices here
  switch($var) {
   ...
  }
}

---

## #3950 — walt boring — 2003-08-14T19:11:42+00:00

URL: https://externals.io/message/3892#3950 · In reply to: #3948

Timm Friebe wrote:

>
>[...]
>  
>
> > […]
>
>Unfortunately it is not:
>
>thekid@friebes:~ > php -r '$a= NULL; var_dump(isset($a));'
>bool(false)
>
>- Timm
>
>
>  
>
exactly why isset() is either
1) a bad name for the function or
2) 'broken'

Since the isset() documentation explicitly warns that it will return 
false if
the value is set and is NULL,  one could say that option 2 is invalid.  So,
in my opinion isset() is really a bad choice for a name of that 
functionality,
and why something like variable_exists() is needed.  

Maybe it could just be called exists() ?

I'm sure changing the functionality of isset() isn't really an option at 
this
point for all of the existing apps that use it.

Walt

---

## #3952 — Lars Torben Wilson — 2003-08-14T19:17:20+00:00

URL: https://externals.io/message/3892#3952 · In reply to: #3936

> Do we really need this function? I see 2 ways of 'implementing' this 
> functionality in PHP without having to add another function. For example:
> (isset($var) || is_null($var)) or gettype($var).
> 
> Ilia

Those ideas don't do the same thing, though. Try it with arrays:

-SNIP------------------------------------------------------------
> > […]
$arr = array(1 => null, 2 => array(1 => 3, 2 => null));

variable_exists($arr[1]): yes (should be yes)
variable_exists($arr[2]): yes (should be yes)
variable_exists($arr[3]): no (should be no)
variable_exists($arr[1][2]): no (should be no)
variable_exists($arr[2][1]): yes (should be yes)
variable_exists($arr[2][2]): yes (should be yes)
variable_exists($arr[2][3]): no (should be no)




> > […]
$arr = array(1 => null, 2 => array(1 => 3, 2 => null));

gettype($arr[1]): yes (should be yes)
gettype($arr[2]): yes (should be yes)

Notice:  Undefined offset:  3 in /home/torben/public_html/phptest/variable_exists.html on line 40

gettype($arr[3]): yes (should be no)
gettype($arr[1][2]): yes (should be no)
gettype($arr[2][1]): yes (should be yes)
gettype($arr[2][2]): yes (should be yes)

Notice:  Undefined offset:  3 in /home/torben/public_html/phptest/variable_exists.html on line 44

gettype($arr[2][3]): yes (should be no)




> > […]
$arr = array(1 => null, 2 => array(1 => 3, 2 => null));

(isset($arr[1]) || is_null($arr[1])): yes (should be yes)
(isset($arr[2]) || is_null($arr[2])): yes (should be yes)


Notice:  Undefined offset:  3 in /home/torben/public_html/phptest/variable_exists.html on line 51

(isset($arr[3]) || is_null($arr[3])): yes (should be no)
(isset($arr[1][2]) || is_null($arr[1][2])): yes (should be no)
(isset($arr[2][1]) || is_null($arr[2][1])): yes (should be yes)
(isset($arr[2][2]) || is_null($arr[2][2])): yes (should be yes)


Notice:  Undefined offset:  3 in /home/torben/public_html/phptest/variable_exists.html on line 55

(isset($arr[2][3]) || is_null($arr[2][3])): yes (should be no)
-SNIP------------------------------------------------------------



-- 
 Torben Wilson <torben@php.net>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====

---

## #3951 — Ilia Alshanetsky — 2003-08-14T19:30:20+00:00

URL: https://externals.io/message/3892#3951 · In reply to: #3950

> exactly why isset() is either
> 1) a bad name for the function or
> 2) 'broken'

Wrong. isset() has a very proper name "is-set", which to me and other people 
says that it will see if a variable is set (has a value). It does this job 
admirably and it certainly not broken. Documentation specifies how this 
function will perform perfectly.

Ilia

---

## #3956 — Lars Torben Wilson — 2003-08-14T19:32:01+00:00

URL: https://externals.io/message/3892#3956 · In reply to: #3937

> I am not really convinced either that variable_exists() is 
> `function_exists()` parallel.
> Under what circumstances is this needed?
> 
> Andi

The followup I sent to Ilia gives examples of how this patch can be
used to determine whether, for instance, array keys exist, without
having to use the `array_key_exists()` workaround; also, you can check
whether an object has defined an attribute, even if it hasn't yet
assigned it a value:

<?php
class foo {
    var $bar;
    var $baz = null;
    var $quux = 'quux';
}
$foo = new foo;

echo "variable_exists(\$foo-&gt;bar): " . (variable_exists($foo->bar) ?
'yes' : 'no') . " (should be yes)\n";
echo "variable_exists(\$foo-&gt;baz): " . (variable_exists($foo->baz) ?
'yes' : 'no') . " (should be yes)\n";
echo "variable_exists(\$foo-&gt;quux): " . (variable_exists($foo->quux)
? 'yes' : 'no') . " (should be yes)\n";
echo "variable_exists(\$foo-&gt;quuux): " .
(variable_exists($foo->quuux) ? 'yes' : 'no') . " (should be no)\n";

echo "isset(\$foo-&gt;bar): " . (isset($foo->bar) ? 'yes' : 'no') . "
(should be yes)\n";
echo "isset(\$foo-&gt;baz): " . (isset($foo->baz) ? 'yes' : 'no') . "
(should be yes)\n";
echo "isset(\$foo-&gt;quux): " . (isset($foo->quux) ? 'yes' : 'no') . "
(should be yes)\n";
echo "isset(\$foo-&gt;quuux): " . (isset($foo->quuux) ? 'yes' : 'no') .
" (should be no)\n";
?>

...the output from the above is:

-----------------------------------------------------------------------
variable_exists($foo->bar): yes (should be yes)
variable_exists($foo->baz): yes (should be yes)
variable_exists($foo->quux): yes (should be yes)
variable_exists($foo->quuux): no (should be no)
isset($foo->bar): no (should be yes)
isset($foo->baz): no (should be yes)
isset($foo->quux): yes (should be yes)
isset($foo->quuux): no (should be no)
-----------------------------------------------------------------------

etc. It's a little annoying at times that that isset() doesn't do what
its name suggests it should, which leads people to try to use it
for things which is seems that it should be able to do, but can't. When
I've tried to explain it, the only answer I can give is "Sorry, that's
just the way it is--and no, you can't do that in PHP".

It seems basic enough functionality that is almost, but not quite,
satisfied by isset(). 


-- 
 Torben Wilson <torben@php.net>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====

---

## #3954 — Robert Cummings — 2003-08-14T19:33:35+00:00

URL: https://externals.io/message/3892#3954 · In reply to: #3951

Actually isset() doesn't behave as it should:

    $foo = null;
    echo isset( $foo );

In the above it should return true, but doesn't because it considers
null to be not set. Thus it is flawed since I explicitly set it to null.
Understandably this is documented; however, the issue here is whether a
variable_exists() function should be added -- for which I am +1 for
adding it :)

Cheers,
Rob.



> 
> > […]
> 
> Wrong. isset() has a very proper name "is-set", which to me and other people 
> says that it will see if a variable is set (has a value). It does this job 
> admirably and it certainly not broken. Documentation specifies how this 
> function will perform perfectly.
> 
> Ilia
> 
> 
> -- 
> 
> 
> 
> 
-- 
.---------------------------------------------.
| Worlds of Carnage - http://www.wocmud.org   |
:---------------------------------------------:
| Come visit a world of myth and legend where |
| fantastical creatures come to life and the  |
| stuff of nightmares grasp for your soul.    |
`---------------------------------------------'

---

## #3955 — Robert Cummings — 2003-08-14T19:34:57+00:00

URL: https://externals.io/message/3892#3955 · In reply to: #3953

Utility is in the eye of the beholder... you may not see where it would
be useful, but I'd argue that many others do.

Cheers,
Rob.


> Correct, it appears my php work around may not work as a undefined variable 
> would gain a `NULL` value as soon as it's used, making `is_null()` always return 
> true. That said, I still do not see a situation where such a function would 
> be useful. 


-- 
.---------------------------------------------.
| Worlds of Carnage - http://www.wocmud.org   |
:---------------------------------------------:
| Come visit a world of myth and legend where |
| fantastical creatures come to life and the  |
| stuff of nightmares grasp for your soul.    |
`---------------------------------------------'

---

## #3957 — walt boring — 2003-08-14T19:35:22+00:00

URL: https://externals.io/message/3892#3957 · In reply to: #3951

Ilia Alshanetsky wrote:

>
>  
>
> > […]
>
>Wrong. isset() has a very proper name "is-set", which to me and other people 
>says that it will see if a variable is set (has a value). It does this job 
>admirably and it certainly not broken. Documentation specifies how this 
>function will perform perfectly.
>
>Ilia
>
>  
>
Then please explain the logic of why
$var = null

isset($var) returns false.

In your words "it will see if a variable is set (has a value)".

$var IS set and DOES have a value, it just happens to be a value of null.

W

---

## #3958 — Derick Rethans — 2003-08-14T19:41:50+00:00

URL: https://externals.io/message/3892#3958 · In reply to: #3957

> Then please explain the logic of why
> $var = null
> 
> isset($var) returns false.
> 
> In your words "it will see if a variable is set (has a value)".
> 
> $var IS set and DOES have a value, it just happens to be a value of null.

I've done a fair deal of PHP programming and I never had the need to do 
this... What use do you have for a variable with the value NULL? 

Derick

-- 
"Interpreting what the GPL actually means is a job best left to those
                    that read the future by examining animal entrails."
-------------------------------------------------------------------------
 Derick Rethans                                 http://derickrethans.nl/ 
 International PHP Magazine                          http://php-mag.net/

---

## #3953 — Ilia Alshanetsky — 2003-08-14T19:42:58+00:00

URL: https://externals.io/message/3892#3953 · In reply to: #3952

Correct, it appears my php work around may not work as a undefined variable 
would gain a `NULL` value as soon as it's used, making `is_null()` always return 
true. That said, I still do not see a situation where such a function would 
be useful. 

Ilia

---

## #3960 — Lars Torben Wilson — 2003-08-14T19:51:39+00:00

URL: https://externals.io/message/3892#3960 · In reply to: #3959

> 
> > […]
> 
> Not quite. To understand the nature of `NULL` you must consider the following. 
> Suppose you have a variable $foo you wish to 'destroy' you can do so by doing 
> unset($foo) or $foo = NULL;. In both cases the value of $foo will be 
> destroyed, however the variable will remain, it's value will become NULL. 
> Therefor isset() behaviour, which works by seeing if a variable exists and 
> making sure that its value is not null, is correct and now flawed as you 
> claim. This is true for other languages as well such as C, when a pointer's 
> value is null that pointer is 'no set'.
> 
> Ilia

unset($foo) is not the same as $foo = NULL, which is one way this
is useful:

<?php
echo "\$foo = null\n\n";
$foo = null;
echo "variable_exists(\$foo): " . (variable_exists($foo) ? 'yes' : 'no')
. " (should be yes)\n";
echo "isset(\$foo): " . (isset($foo) ? 'yes' : 'no') . " (should be
yes)\n";
echo "unset(\$foo);\n";
unset($foo);
echo "variable_exists(\$foo): " . (variable_exists($foo) ? 'yes' : 'no')
. " (should be no)\n";
echo "isset(\$bar): " . (isset($bar) ? 'yes' : 'no') . " (should be
no)\n";
?>

OUTPUT:
$foo = null
variable_exists($foo): yes (should be yes)
isset($foo): no (should be yes)
unset($foo);
variable_exists($foo): no (should be no)
isset($bar): no (should be no)


-- 
 Torben Wilson <torben@php.net>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====

---

## #3959 — Ilia Alshanetsky — 2003-08-14T19:59:05+00:00

URL: https://externals.io/message/3892#3959 · In reply to: #3954

> Actually isset() doesn't behave as it should:
>
>     $foo = null;
>     echo isset( $foo );

Not quite. To understand the nature of `NULL` you must consider the following. 
Suppose you have a variable $foo you wish to 'destroy' you can do so by doing 
unset($foo) or $foo = NULL;. In both cases the value of $foo will be 
destroyed, however the variable will remain, it's value will become NULL. 
Therefor isset() behaviour, which works by seeing if a variable exists and 
making sure that its value is not null, is correct and now flawed as you 
claim. This is true for other languages as well such as C, when a pointer's 
value is null that pointer is 'no set'.

Ilia

---

## #3962 — walt boring — 2003-08-14T20:22:06+00:00

URL: https://externals.io/message/3892#3962 · In reply to: #3959

>
>
>Not quite. To understand the nature of `NULL` you must consider the following. 
>Suppose you have a variable $foo you wish to 'destroy' you can do so by doing 
>unset($foo) or $foo = NULL;. In both cases the value of $foo will be 
>destroyed, however the variable will remain, it's value will become NULL. 
>
Doing an unset($foo) and $foo=null are two completely different outcomes 
in PHP. 
As you know unset actually destroys the variable from the current scope, 
as well as
the value.  It doesn't exist, nor is it set.
Doing a $foo=null, does not destroy the variable.  It still exists, and 
is set.

unset($foo);

echo $foo;   //<-- you get a nice php Notice, because this variable 
doesn't exist

versus

$foo=nul;

echo $foo;  //no warnings at all because the variable does exist and it 
is set!

>Therefor isset() behaviour, which works by seeing if a variable exists and 
>making sure that its value is not null, is correct and now flawed as you 
>claim. This is true for other languages as well such as C, when a pointer's 
>value is null that pointer is 'no set'.
>
>Ilia
>
>  
>

In C, you can easily say a pointer set to 0x0/null, is in fact set. Are 
you always guarenteed that
char *foo;  is going to be set to 0x0 ?

I would say it is more wise to do
char *foo = null; 
Now you know that the foo pointer is set!  and has a value of null.

At the very least, one could say that isset() is confusing, and not what 
a lot of folks expect it to do. 
Yes, the documentation states the functionality, even if it is confusing.

Walt

---

## #3964 — Lars Torben Wilson — 2003-08-14T20:35:54+00:00

URL: https://externals.io/message/3892#3964 · In reply to: #3958

> 
> 
> > […]
null.
> 
> I've done a fair deal of PHP programming and I never had the need to
do 
> this... What use do you have for a variable with the value NULL? 
> 
> Derick

Well, one might use it to represent a `NULL` value, much like we do with
dbs. I imagine that they had a reason for giving us a way to work with
NULL. Otherwise we'd just destroy the variable and be done with it.

Here's one thing: it's more straightforward to do

  if (variable_exists($arr[1][2][4])) {. . .}

than

  if (array_key_exists(4, $arr[1][2]) {. . .}

...especically since `array_key_exists()` will start spewing notices if
any subarray in the array you pass in does not exist. You can work
around it, but then the chain of workarounds to handle something many
would argue should be basic functionality gets a little over-the-top.


-- 
 Torben Wilson <torben@php.net>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====

---

## #3965 — Stefan Walk — 2003-08-14T21:01:17+00:00

URL: https://externals.io/message/3892#3965 · In reply to: #3949

> It can happen quite easily.  I always develop with full warnings/errors on.

So do I.

> So if for example a var isn't set for whatever reason,  then trying to 
> access the
> variable will throw a php Notice.  variable_exists() would prevent that, 
> as does isset().

Correct, isset works.

> isset() would work for my example below, but it still is a 'broken' 
> function in my opinion.
> 
> if ($var) {  // <-- you'll get a php notice on this line
>  switch ($var) {
>    ...
>  }
> 
> }
> 
> versus doing
> 
> if (variable_exists($var))  {  //no php notices here
>  switch($var) {
>   ...
>  }
> }

As you already stated, isset works also. I asked for an occasion where
your variable_exists would be useful because isset does not work.

DB arguments don't count, if a value is null in a database it is
considered - you guessed it - not set.

I simply don't see a need for this.

-- 
Regards,
Stefan Walk
<swalk@prp.physik.tu-darmstadt.de

---

## #3966 — Lars Torben Wilson — 2003-08-14T21:05:29+00:00

URL: https://externals.io/message/3892#3966 · In reply to: #3965

> 
> > […]
> 
> So do I.
> 
> > […]
> 
> Correct, isset works.
> 
> > […]
> 
> As you already stated, isset works also. I asked for an occasion where
> your variable_exists would be useful because isset does not work.
> 
> DB arguments don't count, if a value is null in a database it is
> considered - you guessed it - not set.
> 
> I simply don't see a need for this.

I have posted several examples, complete with code and output.


-- 
 Torben Wilson <torben@php.net>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====

---

## #3970 — Cristiano Duarte — 2003-08-15T04:47:44+00:00

URL: https://externals.io/message/3892#3970 · In reply to: #3956

+1 for variable_exists

Cristiano Duarte

"Lars Torben Wilson" <torben@php.net> escreveu na mensagem
news:1060889520.1036.45.camel@ali...
> 
> > […]
>
> The followup I sent to Ilia gives examples of how this patch can be
> used to determine whether, for instance, array keys exist, without
> having to use the `array_key_exists()` workaround; also, you can check
> whether an object has defined an attribute, even if it hasn't yet
> assigned it a value:
>
> <?php
> class foo {
>     var $bar;
>     var $baz = null;
>     var $quux = 'quux';
> }
> $foo = new foo;
>
> echo "variable_exists(\$foo-&gt;bar): " . (variable_exists($foo->bar) ?
> 'yes' : 'no') . " (should be yes)\n";
> echo "variable_exists(\$foo-&gt;baz): " . (variable_exists($foo->baz) ?
> 'yes' : 'no') . " (should be yes)\n";
> echo "variable_exists(\$foo-&gt;quux): " . (variable_exists($foo->quux)
> ? 'yes' : 'no') . " (should be yes)\n";
> echo "variable_exists(\$foo-&gt;quuux): " .
> (variable_exists($foo->quuux) ? 'yes' : 'no') . " (should be no)\n";
>
> echo "isset(\$foo-&gt;bar): " . (isset($foo->bar) ? 'yes' : 'no') . "
> (should be yes)\n";
> echo "isset(\$foo-&gt;baz): " . (isset($foo->baz) ? 'yes' : 'no') . "
> (should be yes)\n";
> echo "isset(\$foo-&gt;quux): " . (isset($foo->quux) ? 'yes' : 'no') . "
> (should be yes)\n";
> echo "isset(\$foo-&gt;quuux): " . (isset($foo->quuux) ? 'yes' : 'no') .
> " (should be no)\n";
> ?>
>
> ...the output from the above is:
>
> -----------------------------------------------------------------------
> variable_exists($foo->bar): yes (should be yes)
> variable_exists($foo->baz): yes (should be yes)
> variable_exists($foo->quux): yes (should be yes)
> variable_exists($foo->quuux): no (should be no)
> isset($foo->bar): no (should be yes)
> isset($foo->baz): no (should be yes)
> isset($foo->quux): yes (should be yes)
> isset($foo->quuux): no (should be no)
> -----------------------------------------------------------------------
>
> etc. It's a little annoying at times that that isset() doesn't do what
> its name suggests it should, which leads people to try to use it
> for things which is seems that it should be able to do, but can't. When
> I've tried to explain it, the only answer I can give is "Sorry, that's
> just the way it is--and no, you can't do that in PHP".
>
> It seems basic enough functionality that is almost, but not quite,
> satisfied by isset().
>
>
> --
>  Torben Wilson <torben@php.net>                        +1.604.709.0506
>  http://www.thebuttlesschaps.com          http://www.inflatableeye.com
>  http://www.hybrid17.com                  http://www.themainonmain.com
>  -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====

---

## #3996 — Cristiano Duarte — 2003-08-15T18:12:01+00:00

URL: https://externals.io/message/3892#3996

<nicos@php.net> escreveu na mensagem
news:20030815074124.50073.qmail@pb1.pair.com...>
> You can avoid the notice by doing if(isset($var) && !is_null($var)) { }
>
> We can't have a function for every piece of code.
>
IMHO, a function to test if a variable exists or not is not "every piece of
code". See what is necessary to test if a variable exists or not !!! In fact
it's a workaround to a necessary variable_exists function.

Cristiano Duarte

---

## #4033 — Ard Biesheuvel — 2003-08-16T22:39:38+00:00

URL: https://externals.io/message/3892#4033 · In reply to: #3892

> A few weeks ago I submitted a patch in the bug db for a
> variable_exists() construct, which parallels the `function_exists()`
> one but for variables. In short, it returns `TRUE` if a variable
> exists, regardless of its value. In other words, it's an isset()
> which doesn't care if the variable's value is NULL.

Nice work.

However, if your application relies on unset variables to be
distinguishable from variables that are null, you should
probably fix your application first, and then consider 'fixing'
the language.

Ard
