variable_exists() patch

php.internals

Lars Torben Wilson

23 years ago
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 ====-----

Unnamed Person

23 years ago
"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 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.
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.

Timm Friebe

23 years ago
On Thu, 2003-08-14 at 13:18, nicos@php.net wrote: [...]
> 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

Walt Boring

23 years ago
Timm Friebe wrote:
>On Thu, 2003-08-14 at 13:18, nicos@php.net wrote: >[...] > > >>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 > > > >
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

Ilia A.

23 years ago
On August 14, 2003 03:11 pm, walt boring wrote:
> 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

Robert Cummings

23 years ago
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. On Thu, 2003-08-14 at 15:30, Ilia Alshanetsky wrote:
> On August 14, 2003 03:11 pm, walt boring wrote: > > 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 > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
-- .---------------------------------------------. | 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. | `---------------------------------------------'

Ilia A.

23 years ago
On August 14, 2003 03:33 pm, Robert Cummings wrote:
> 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

Lars Torben Wilson

23 years ago
On Thu, 2003-08-14 at 12:59, Ilia Alshanetsky wrote:
> On August 14, 2003 03:33 pm, Robert Cummings wrote: > > 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
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 ====-----

Walt Boring

23 years ago
> > >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

Walt Boring

23 years ago
Ilia Alshanetsky wrote:
>On August 14, 2003 03:11 pm, walt boring wrote: > > >>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 > > >
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

Derick Rethans

23 years ago
On Thu, 14 Aug 2003, walt boring wrote:
> 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/ -------------------------------------------------------------------------

Lars Torben Wilson

23 years ago
On Thu, 2003-08-14 at 12:41, Derick Rethans wrote:
> On Thu, 14 Aug 2003, walt boring wrote: > > > 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
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 ====-----

Ilia A.

23 years ago
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 On August 13, 2003 06:25 pm, Lars Torben Wilson wrote:

Unnamed Person

23 years ago
"Ilia Alshanetsky" <ilia@prohost.org> a écrit dans le message de news:200308141145.38495.ilia@prohost.org...
> 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).
Sure its really _enough_. We can't have a function for every little script.

Walt Boring

23 years ago
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

Stefan Walk

23 years ago
On Thu, Aug 14, 2003 at 11:15:51AM -0700, walt boring 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.
-- Regards, Stefan Walk <swalk@prp.physik.tu-darmstadt.de>

Walt Boring

23 years ago
Stefan Walk wrote:
>On Thu, Aug 14, 2003 at 11:15:51AM -0700, walt boring 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) { ... } }

Stefan Walk

23 years ago
On Thu, Aug 14, 2003 at 12:00:26PM -0700, walt boring wrote:
> 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>

Lars Torben Wilson

23 years ago
On Thu, 2003-08-14 at 14:01, Stefan Walk wrote:
> On Thu, Aug 14, 2003 at 12:00:26PM -0700, walt boring wrote: > > 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 ====-----

Unnamed Person

23 years ago
"Walt Boring" <waboring@3gstech.com> a écrit dans le message de news:3F3BDC4A.20603@3gstech.com...
> Stefan Walk wrote: > > >On Thu, Aug 14, 2003 at 11:15:51AM -0700, walt boring 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) { > ... > } > }
You can avoid the notice by doing if(isset($var) && !is_null($var)) { } We can't have a function for every piece of code.

Cristiano Duarte

23 years ago
<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

Lars Torben Wilson

23 years ago
On Thu, 2003-08-14 at 08:45, 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
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 ====-----

Ilia A.

23 years ago
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

Robert Cummings

23 years ago
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. On Thu, 2003-08-14 at 15:42, Ilia Alshanetsky wrote:
> 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. | `---------------------------------------------'

Zeev Suraski

23 years ago
At 21:15 14/08/2003, walt boring wrote:
>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.
It's just that 'null' is not a value. The way PHP treats null value is identical to that of non existent values, which is precisely why isset() behaves the way that it does. I'm not sure what the logic is behind Ilia's alternatives, they won't behave any differently whether $var exists and is null, or whether it doesn't exist at all, and it's intentional. Zeev

Lars Torben Wilson

23 years ago
On Thu, 2003-08-14 at 16:27, Zeev Suraski wrote:
> At 21:15 14/08/2003, walt boring wrote: > >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. > > It's just that 'null' is not a value. > > The way PHP treats null value is identical to that of non existent values, > which is precisely why isset() behaves the way that it does. I'm not sure > what the logic is behind Ilia's alternatives, they won't behave any > differently whether $var exists and is null, or whether it doesn't exist at > all, and it's intentional. > > Zeev
Right--I wouldn't suggest that isset() be changed. But variable_exists() would (for instance) allow the programmer to clear up the weirdness introduced when they run into the fact that assigning NULL to a value causes it for all intents and purposes to no longer exist--unless that variable is an array element, in which case it does indeed still exist (as it must, e.g. in order to preserver db results of NULL) as can be proved with array_key_exists(). The other thing is that although assigning NULL to a value causes isset() to claim that the variable is not set, testing the value of a variable set to NULL does not behave the way a truly non-existent variable does (i.e. it won't throw a notice if you read from a NULL-assigned variable). All variable_exists() does is give a simple, completely consistent way to check for the existence of a variable, regardless of its value.
-- 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 ====-----

Alan Knowles

23 years ago
I hit this a couple of months ago.. trying to implement NULL support in dataobjects: $do = DB_DataObject::factory('test'); $do->get(12); $do->birthday = null; $do->update(); was supposed to generate SELECT * FROM test WHERE id=12; UPDATE test SET birthday=NULL where id = 12; or $do = DB_DataObject::factory('test'); $do->birthday = null; $do->find(); to do SELECT * FROM test WHERE birthday IS NULL; but since there was no effective way to detect null, as apposed to unset.. I had to give up... - If this could be solved by variable_exists() - even though var $birthday is defined.. It would be great.. Regards Alan

Thies C. Arntzen

23 years ago
On Fri, Aug 15, 2003 at 07:41:48AM +0800, Alan Knowles wrote:
> I hit this a couple of months ago.. trying to implement NULL > support in dataobjects: > > $do = DB_DataObject::factory('test'); > $do->get(12); > $do->birthday = null; > $do->update(); > > was supposed to generate > SELECT * FROM test WHERE id=12; > UPDATE test SET birthday=NULL where id = 12; > > or > > $do = DB_DataObject::factory('test'); > $do->birthday = null; > $do->find(); > > to do > SELECT * FROM test WHERE birthday IS NULL; > > but since there was no effective way to detect null, as apposed to > unset.. I had to give up... - If this could be solved by > variable_exists() - even though var $birthday is defined.. It would be > great..
alan, you hit the "nail on the head" - zeev, do you see any way to solve alans problem? re, thies

Stefan Walk

23 years ago
On Fri, Aug 15, 2003 at 11:36:00AM +0200, Thies C. Arntzen wrote:
> On Fri, Aug 15, 2003 at 07:41:48AM +0800, Alan Knowles wrote: > > I hit this a couple of months ago.. trying to implement NULL > > support in dataobjects: > > > > $do = DB_DataObject::factory('test'); > > $do->get(12); > > $do->birthday = null; > > $do->update(); > > > > was supposed to generate > > SELECT * FROM test WHERE id=12; > > UPDATE test SET birthday=NULL where id = 12; > > > > or > > > > $do = DB_DataObject::factory('test'); > > $do->birthday = null; > > $do->find(); > > > > to do > > SELECT * FROM test WHERE birthday IS NULL; > > > > but since there was no effective way to detect null, as apposed to > > unset.. I had to give up... - If this could be solved by > > variable_exists() - even though var $birthday is defined.. It would be > > great.. > > alan, > > you hit the "nail on the head" - zeev, do you see any way to > solve alans problem? > > re, > thies
class Foo { function dump() { var_dump(array_key_exists('bar', (array)$this)); } } $foo = new Foo; $foo->bar = null; $foo->dump(); // => bool(true) It's not that hard to detect.
-- Regards, Stefan Walk <swalk@prp.physik.tu-darmstadt.de>

Cristiano Duarte

23 years ago
"Stefan Walk" <swalk@prp.physik.tu-darmstadt.de> escreveu na mensagem news:20030816075011.GA3576@prp0.prp.physik.tu-darmstadt.de...
> class Foo { > function dump() { > var_dump(array_key_exists('bar', (array)$this)); > } > } > $foo = new Foo; > $foo->bar = null; > $foo->dump(); > // => bool(true) > > It's not that hard to detect.
IMHO, array_key_exists can be used this way, but it shouldn't. The function name has a meaning specific for arrays. We must have a not so specific way to detect if a variable(or attribute) exists or not. Cristiano Duarte.

Andrey Hristov

23 years ago
oooh, here is easy sollution for global vars : <?php var_dump(array_key_exists('a',$GLOBALS)); $a = NULL; var_dump(array_key_exists('a',$GLOBALS)); ?> However there is no $LOCALS in the local scope. Andrey ----- Original Message ----- From: "Cristiano Duarte" <cunha17@uol.com.br> To: <internals@lists.php.net> Sent: Saturday, August 16, 2003 7:26 PM Subject: Re: [PHP-DEV] variable_exists() patch
> "Stefan Walk" <swalk@prp.physik.tu-darmstadt.de> escreveu na mensagem > news:20030816075011.GA3576@prp0.prp.physik.tu-darmstadt.de... > > class Foo { > > function dump() { > > var_dump(array_key_exists('bar', (array)$this)); > > } > > } > > $foo = new Foo; > > $foo->bar = null; > > $foo->dump(); > > // => bool(true) > > > > It's not that hard to detect. > IMHO, array_key_exists can be used this way, but it shouldn't. The
function

Stefan Walk

23 years ago
On Sat, Aug 16, 2003 at 07:30:27PM +0300, Andrey Hristov wrote:
> However there is no $LOCALS in the local scope.
There is, sort of: http://www.php.net/get_defined_vars
-- Regards, Stefan Walk <swalk@prp.physik.tu-darmstadt.de>

fabrice

23 years ago
You may consider to use "empty" function that check if isset and not NULL And also "!empty" inverse function. Empty($var) ? DoSomethingIfNotSetOrNull() : DoOther(); Also it is true, I use a personnal function to check var ... Something like that Function variable_exists( $var ) { $return = empty($var) ? null : $var; return $return; } So my function check if var isset and not null and return null if not or directly $var if false( = isset and not null in this case) So !empty($var) = ( isset($var) AND !is_null($var) ) And, Empty($var) = ( !isset($var) AND is_null($var) ) Function : empty ---------------------------------------- Inverse function : !empty Syntax : #bool empty( #mixed <var>) ---------------------------------------- Arguments : #mixed <var> Return : #bolean <true||false> ---------------------------------------- Note : empty is not really a function but a language structure, look at php.net for more info -----Message d'origine----- De : Stefan Walk [mailto:swalk@prp.physik.tu-darmstadt.de] Envoyé : samedi 16 août 2003 09:50 À : Thies C. Arntzen Cc : Alan Knowles; Lars Torben Wilson; zeev@zend.com; walt boring; ilia@prohost.org; internals@lists.php.net Objet : Re: [PHP-DEV] variable_exists() patch On Fri, Aug 15, 2003 at 11:36:00AM +0200, Thies C. Arntzen wrote:
> On Fri, Aug 15, 2003 at 07:41:48AM +0800, Alan Knowles wrote: > > I hit this a couple of months ago.. trying to implement NULL support > > in dataobjects: > > > > $do = DB_DataObject::factory('test'); $do->get(12); $do->birthday = > > null; $do->update(); > > > > was supposed to generate > > SELECT * FROM test WHERE id=12; > > UPDATE test SET birthday=NULL where id = 12; > > > > or > > > > $do = DB_DataObject::factory('test'); $do->birthday = null; > > $do->find(); > > > > to do > > SELECT * FROM test WHERE birthday IS NULL; > > > > but since there was no effective way to detect null, as apposed to > > unset.. I had to give up... - If this could be solved by > > variable_exists() - even though var $birthday is defined.. It would > > be great.. > > alan, > > you hit the "nail on the head" - zeev, do you see any way to > solve alans problem? > > re, > thies
class Foo { function dump() { var_dump(array_key_exists('bar', (array)$this)); } } $foo = new Foo; $foo->bar = null; $foo->dump(); // => bool(true) It's not that hard to detect.
-- Regards, Stefan Walk <swalk@prp.physik.tu-darmstadt.de> -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Zeev Suraski

23 years ago
At 12:36 15/08/2003, Thies C. Arntzen wrote:
>On Fri, Aug 15, 2003 at 07:41:48AM +0800, Alan Knowles wrote: > > I hit this a couple of months ago.. trying to implement NULL > > support in dataobjects: > > > > $do = DB_DataObject::factory('test'); > > $do->get(12); > > $do->birthday = null; > > $do->update(); > > > > was supposed to generate > > SELECT * FROM test WHERE id=12; > > UPDATE test SET birthday=NULL where id = 12; > > > > or > > > > $do = DB_DataObject::factory('test'); > > $do->birthday = null; > > $do->find(); > > > > to do > > SELECT * FROM test WHERE birthday IS NULL; > > > > but since there was no effective way to detect null, as apposed to > > unset.. I had to give up... - If this could be solved by > > variable_exists() - even though var $birthday is defined.. It would be > > great.. > > alan, > > you hit the "nail on the head" - zeev, do you see any way to > solve alans problem?
Yep, there are several options, but using the language-level NULL is probably not one of them. You could have a setNull() method that defines the NULL values. You could have DB_DataObject::null_value defined to some value that is not used in SQL (e.g., an object) and use it instead. There are probably other ways as well... Zeev

Walt Boring

23 years ago
> > >> > $do = DB_DataObject::factory('test'); >> > $do->birthday = null; >> > $do->find(); >> > >> > to do >> > SELECT * FROM test WHERE birthday IS NULL; >> > >> > but since there was no effective way to detect null, as apposed to >> > unset.. I had to give up... - If this could be solved by >> > variable_exists() - even though var $birthday is defined.. It would be >> > great.. >> >> alan, >> >> you hit the "nail on the head" - zeev, do you see any way to >> solve alans problem? > > > > > Yep, there are several options, but using the language-level NULL is > probably not one of them. You could have a setNull() method that > defines the NULL values. You could have DB_DataObject::null_value > defined to some value that is not used in SQL (e.g., an object) and > use it instead. There are probably other ways as well... > > Zeev >
This seems like confusion and bugs waiting to happen. Forcing the user to do something counter intuitive like : $do->birthday = DB_DataObject::setNull(); vs. $do->birthday = null; This just begs for mistakes to happen using actuall null, since it is used everywhere else within PHP. People will forget they have to use a 'hacked' setNull() method, just to set something to a logical null value. This doesn't seem like a good 'solution' to me. People will want to do $foo = null; Walt

Robert Cummings

23 years ago
On Thu, 2003-08-14 at 19:27, Zeev Suraski wrote:
> At 21:15 14/08/2003, walt boring wrote: > > The way PHP treats null value is identical to that of non existent values, > which is precisely why isset() behaves the way that it does. I'm not sure > what the logic is behind Ilia's alternatives, they won't behave any > differently whether $var exists and is null, or whether it doesn't exist at > all, and it's intentional. > > Zeev
Sorry to possibly riding in late on this but my power was out for a couple of days :) Anyways, if PHP treats the null value identical to that of non-existent value then why does the following not generate a warning? $foo = null; echo $foo; whereas a non-existent value does produce an warning: echo $fee; I think the issue here is that because PHP allows developers to use the null value as any other literal value, that it has been used in this way, and not as you might have originally hoped. Thus null is a valid value and for many developers (myself included) it doesn't not simply represent the non-existence of a value. Also to exascerbate the problem further, if setting a value to null is the same as unsetting the value, then why do I get the following odd behaviour? $foo = array( 1, 2, 3, 4, 5 ); $foo[2] = null; foreach( $foo as $key => $value ) { echo $key.' -> '.$value."\n"; } Output: 0 -> 1 1 -> 2 2 -> 3 -> 4 4 -> 5 So it seems to me that a null value does not define the non-existence of a value, but is itself actually a value. And this as far as I am concerned is quite valid. Cheers, Rob.
-- .---------------------------------------------. | 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. | `---------------------------------------------'

Cristiano Duarte

23 years ago
"Robert Cummings" <robert@wocmud.org> escreveu na mensagem news:1061041995.2465.12.camel@blobule.suds...
> So it seems to me that a null value does not define the non-existence of > a value, but is itself actually a value. And this as far as I am > concerned is quite valid.
Shure. That's one more reason for variable_exists. Cristiano Duarte

Zeev Suraski

23 years ago
At 16:53 16/08/2003, Robert Cummings wrote:
>So it seems to me that a null value does not define the non-existence of >a value, but is itself actually a value. And this as far as I am >concerned is quite valid.
Ok, but it still doesn't change the fact that null is not a value :) The fact we couldn't make the implementation perfect doesn't change that in any way. I mentioned ways to implement the desired behavior, others also mentioned other ways - but there's no way I'd support adding functionality that will encourage inconsistency in the language. Zeev

Lars Torben Wilson

23 years ago
On Sat, 2003-08-16 at 14:41, Zeev Suraski wrote:
> At 16:53 16/08/2003, Robert Cummings wrote: > >So it seems to me that a null value does not define the non-existence of > >a value, but is itself actually a value. And this as far as I am > >concerned is quite valid. > > Ok, but it still doesn't change the fact that null is not a value :) The > fact we couldn't make the implementation perfect doesn't change that in any > way. > I mentioned ways to implement the desired behavior, others also mentioned > other ways - but there's no way I'd support adding functionality that will > encourage inconsistency in the language. > > Zeev
But the change would make it more consistent, by recognizing that a variable with a NULL value is still defined. Pretending that $foo = null and unset($foo) are the same thing is quite inconsistent and confusing for many, when there are ways (i.e. get_defined_vars()) to prove that they are not the same.
-- 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 ====-----

Stefan Walk

23 years ago
On Sat, Aug 16, 2003 at 02:42:17PM -0700, Lars Torben Wilson wrote:
> But the change would make it more consistent, by recognizing that a > variable with a NULL value is still defined. Pretending that $foo = > null and unset($foo) are the same thing is quite inconsistent and > confusing for many, when there are ways (i.e. get_defined_vars()) to > prove that they are not the same.
You can see that they are meant to be the same by doing $foo = (unset)$foo; ... $foo is null afterwards :) Anyway, variable_exists($foo) is just a synonym for in_array('foo', array_keys(get_defined_vars())) which is not too much trouble to type IMO. *shrug*
-- Regards, Stefan Walk <swalk@prp.physik.tu-darmstadt.de>

Robert Cummings

23 years ago
Actually they are not the same, as this code illistrates: $foo = array( 1, 2, 3, 4, 5 ); unset( $foo[1] ); foreach( $foo as $key => $value ) { echo $foo.' -> '.$value."\n"; } Output: 0 -> 1 2 -> 3 3 -> 4 4 -> 5 in contrast to: $foo = array( 1, 2, 3, 4, 5 ); $foo[2] = null; foreach( $foo as $key => $value ) { echo $key.' -> '.$value."\n"; } Output: 0 -> 1 1 -> 2 2 -> 3 -> 4 4 -> 5 As you can see the entry is not null, it is completely missing. And while you might think that variable_exists($foo) is a synonym for in_array in_array('foo', array_keys(get_defined_vars())) I would strongly argue otherwise. Maybe you should see how that whole evaluation works. Retrieving the keys of an array with say 100 entries would require the copying of 100 key values to zvalues, and then returning a new array. This new array would subsequently be searched for the value, which of course is always slower than looking for the key itself. Whereas variable_exists() would perform a single lookup on the key and not require the copying of keys to zvalues. One is MUCH more efficient than the other. Cheers, Rob. On Sat, 2003-08-16 at 17:51, Stefan Walk wrote:
> On Sat, Aug 16, 2003 at 02:42:17PM -0700, Lars Torben Wilson wrote: > > But the change would make it more consistent, by recognizing that a > > variable with a NULL value is still defined. Pretending that $foo = > > null and unset($foo) are the same thing is quite inconsistent and > > confusing for many, when there are ways (i.e. get_defined_vars()) to > > prove that they are not the same. > > You can see that they are meant to be the same by doing > $foo = (unset)$foo; ... $foo is null afterwards :) > Anyway, > variable_exists($foo) > is just a synonym for > in_array('foo', array_keys(get_defined_vars())) > which is not too much trouble to type IMO. *shrug*
-- .---------------------------------------------. | 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. | `---------------------------------------------'

Stefan Walk

23 years ago
On Sat, Aug 16, 2003 at 06:06:41PM -0400, Robert Cummings wrote:
> As you can see the entry is not null, it is completely missing.
You should use var_dump instead of print_r.
-- Regards, Stefan Walk <swalk@prp.physik.tu-darmstadt.de>

Stefan Walk

23 years ago
Oh, you meant the first output, nevermind. I /know/ unset() and setting something null is not the same - as probably everyone on this list. As to your "Is not the same" argument: Doing something in PHP is of course slower as a language feature. But that's normal. Do you want to have a native function for everything?
-- Regards, Stefan Walk <swalk@prp.physik.tu-darmstadt.de>

Robert Cummings

23 years ago
Understandably PHP is slower than say C :) Generally I don't think there is need for everything to be a native function, however, that said, when the speed gain and utility are great in comparison to doing it as pure PHP then I think the option should be investigated. The current isset() function fails many developers from what I can see, and I know I've experienced this problem before, and so I think a more appropriate function would be nice. Also I'm sure that native function lookup occurs extremely fast, probably even at compile time, thus i would say as the number of native functions increases the impact becomes less and less on execution time. There are already hundreds of native functions, I can only assume one more would have no bearing or at worst extremely minimal impact, but would provide a useful function to many PHP developers who would find it useful. I'm guessing the current split on this thread is somewhere around 50/50 and so it can't be an entirely useless idea. Cheers, Rob. On Sat, 2003-08-16 at 18:19, Stefan Walk wrote:
> Oh, you meant the first output, nevermind. I /know/ unset() and setting > something null is not the same - as probably everyone on this list. > > As to your "Is not the same" argument: > Doing something in PHP is of course slower as a language feature. But > that's normal. Do you want to have a native function for everything? > -- > Regards, > Stefan Walk > <swalk@prp.physik.tu-darmstadt.de> > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
-- .---------------------------------------------. | 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. | `---------------------------------------------'

Robert Cummings

23 years ago
Yes, pretending that it is currently consistent is the problem I think. By introducing the new function we solve the inconsistency. True there are ways around this, such as get_defined_vars(). Interestingly, yet another inconsistency is that get_defined_vars() also returns a variable with a null value. Wouldn't it just be simpler to acquiesce to the fact that null wasn't implemented as you intended and thus has a greater scope of usage now than originally expected? Part of the evolutionary process of any entity is when it exceeds its expectations. Also I'm guessing that get_defined_vars() does not return a reference to an already existing array and thus would be like using a pile driver to hammer a nail in -- correct me if I'm wrong since efficiency wise I'm happy to use that method provided I'm not copying the whole array. Cheers, Rob. On Sat, 2003-08-16 at 17:42, Lars Torben Wilson wrote:
> On Sat, 2003-08-16 at 14:41, Zeev Suraski wrote: > > At 16:53 16/08/2003, Robert Cummings wrote: > > >So it seems to me that a null value does not define the non-existence of > > >a value, but is itself actually a value. And this as far as I am > > >concerned is quite valid. > > > > Ok, but it still doesn't change the fact that null is not a value :) The > > fact we couldn't make the implementation perfect doesn't change that in any > > way. > > I mentioned ways to implement the desired behavior, others also mentioned > > other ways - but there's no way I'd support adding functionality that will > > encourage inconsistency in the language. > > > > Zeev > > But the change would make it more consistent, by recognizing that a > variable with a NULL value is still defined. Pretending that $foo = > null and unset($foo) are the same thing is quite inconsistent and > confusing for many, when there are ways (i.e. get_defined_vars()) to > prove that they are not the same. > > > -- > 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 ====----- > > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
-- .---------------------------------------------. | 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. | `---------------------------------------------'

Andi Gutmans

23 years ago
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:

Unnamed Person

23 years ago
----- Original Message ----- From: "Andi Gutmans" <andi@zend.com> To: <nicos@php.net>; <internals@lists.php.net> Sent: Thursday, August 14, 2003 5:42 PM Subject: Re: [PHP-DEV] Re: variable_exists() patch <snip> Yes in fact I'm not either. I'm not sure this is needed at all. Regards. M.CHAILLAN Nicolas nicos@php.net www.WorldAKT.com Hébergement de sites internets.

Lars Torben Wilson

23 years ago
On Thu, 2003-08-14 at 08:42, Andi Gutmans wrote:
> 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 ====-----

Cristiano Duarte

23 years ago
+1 for variable_exists Cristiano Duarte "Lars Torben Wilson" <torben@php.net> escreveu na mensagem news:1060889520.1036.45.camel@ali...

Ard Biesheuvel

23 years ago
> 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