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 ====
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...Hi out there,
A few weeks ago I submitted a patch in the bug db for a
variable_exists() construct, which parallels thefunction_exists()
one but for variables. In short, it returnsTRUEif 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.Hmm, I thought a variable is set even if its value is NULL?
Are you sure we want this patch?
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 ====-------
Regards.
M.CHAILLAN Nicolas
nicos@php.net
www.WorldAKT.com Hébergement de sites internets.
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->bar): " . (variable_exists($foo->bar) ?
'yes' : 'no') . " (should be yes)\n";
echo "variable_exists($foo->baz): " . (variable_exists($foo->baz) ?
'yes' : 'no') . " (should be yes)\n";
echo "variable_exists($foo->quux): " . (variable_exists($foo->quux)
? 'yes' : 'no') . " (should be yes)\n";
echo "variable_exists($foo->quuux): " .
(variable_exists($foo->quuux) ? 'yes' : 'no') . " (should be no)\n";
echo "isset($foo->bar): " . (isset($foo->bar) ? 'yes' : 'no') . "
(should be yes)\n";
echo "isset($foo->baz): " . (isset($foo->baz) ? 'yes' : 'no') . "
(should be yes)\n";
echo "isset($foo->quux): " . (isset($foo->quux) ? 'yes' : 'no') . "
(should be yes)\n";
echo "isset($foo->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 ====
+1 for variable_exists
Cristiano Duarte
"Lars Torben Wilson" torben@php.net escreveu na mensagem
news:1060889520.1036.45.camel@ali...
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 thearray_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->bar): " . (variable_exists($foo->bar) ?
'yes' : 'no') . " (should be yes)\n";
echo "variable_exists($foo->baz): " . (variable_exists($foo->baz) ?
'yes' : 'no') . " (should be yes)\n";
echo "variable_exists($foo->quux): " . (variable_exists($foo->quux)
? 'yes' : 'no') . " (should be yes)\n";
echo "variable_exists($foo->quuux): " .
(variable_exists($foo->quuux) ? 'yes' : 'no') . " (should be no)\n";echo "isset($foo->bar): " . (isset($foo->bar) ? 'yes' : 'no') . "
(should be yes)\n";
echo "isset($foo->baz): " . (isset($foo->baz) ? 'yes' : 'no') . "
(should be yes)\n";
echo "isset($foo->quux): " . (isset($foo->quux) ? 'yes' : 'no') . "
(should be yes)\n";
echo "isset($foo->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 ====
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 thefunction_exists()
one but for variables. In short, it returnsTRUEif 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 ====
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
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
Stefan Walk wrote:
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.
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) {
...
}
}
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
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.
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 ====
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------------------------------------------------------------
Array testing with variable_exists()
$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)
Array testing with
gettype()
$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)
Array testing with (isset() ||
is_null())
$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 ====
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
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 aNULLvalue as soon as it's used, makingis_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. |
`---------------------------------------------'
A few weeks ago I submitted a patch in the bug db for a
variable_exists() construct, which parallels thefunction_exists()
one but for variables. In short, it returnsTRUEif 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