Inconsistency of empty() and isset() ?

php.internals

Unnamed Person

20 years ago
The following is a direct excerpt from the PHP manual on empty, and isset: bool *empty* ( mixed var ) bool *isset* ( mixed var [, mixed var [, ...]] ) Is there a reason empty does not allow multiple variables at a time, as isset? Was there thought behind it, or is it just an inconsistency?

Unnamed Person

20 years ago
Regardless of if its an inconsistency, I created a patch for it. Here are the files: http://repository.itrebal.com/php/patches/empty/empty.php (Test file) http://repository.itrebal.com/php/patches/empty/patch ------------------------------- Graham Christensen www.itrebal.com www.iamgraham.net

Derick Rethans

20 years ago
On Sat, 15 Apr 2006 itrebal@gmail.com wrote:
> The following is a direct excerpt from the PHP manual on empty, and isset: > bool *empty* ( mixed var ) > bool *isset* ( mixed var [, mixed var [, ...]] ) > Is there a reason empty does not allow multiple variables at a time, as > isset? Was there thought behind it, or is it just an inconsistency?
There is a thought about it, and that is that we could not decide whether it should be an AND or an OR test between the different parameters. Derick

Lukas Smith

20 years ago
Derick Rethans wrote:
> On Sat, 15 Apr 2006 itrebal@gmail.com wrote: > >> The following is a direct excerpt from the PHP manual on empty, and isset: >> bool *empty* ( mixed var ) >> bool *isset* ( mixed var [, mixed var [, ...]] ) >> Is there a reason empty does not allow multiple variables at a time, as >> isset? Was there thought behind it, or is it just an inconsistency? > > There is a thought about it, and that is that we could not decide > whether it should be an AND or an OR test between the different > parameters.
well it seems to be AND for isset() .. which is probably its closest "sibiling" .. so the obvious call when going for consistency would be to AND in empty() as well .. regards, Lukas

Etienne Kneuss

20 years ago
As empty is "quite" the contrary of isset, the OR would be preferable over AND. Indeed: if(!empty($var1) && !empty($var2)) could be simplified to if(!empty($var1, $var2)) Regards.
-- Etienne Kneuss http://www.colder.ch/ colder@php.net

Richard Lynch

20 years ago
On Sun, April 16, 2006 6:25 pm, Etienne Kneuss wrote:
> As empty is "quite" the contrary of isset, the OR would be preferable > over AND.
Sorry, folks! I meant the other way 'round... Whatever it is you check with a zillion form inputs, that's what you'd want. When would you need it the other way with a lot of inputs?
-- Like Music? http://l-i-e.com/artists.htm

Pierre Joye

20 years ago
On 4/17/06, Lukas Smith <lsmith@php.net> wrote:
> Derick Rethans wrote: > > On Sat, 15 Apr 2006 itrebal@gmail.com wrote: > > > >> The following is a direct excerpt from the PHP manual on empty, and isset: > >> bool *empty* ( mixed var ) > >> bool *isset* ( mixed var [, mixed var [, ...]] ) > >> Is there a reason empty does not allow multiple variables at a time, as > >> isset? Was there thought behind it, or is it just an inconsistency? > > > > There is a thought about it, and that is that we could not decide > > whether it should be an AND or an OR test between the different > > parameters. > > well it seems to be AND for isset() .. which is probably its closest > "sibiling" .. so the obvious call when going for consistency would be to > AND in empty() as well ..
Indeed, especially as everyone will tell you that isset and empty are equivalent. --Pierre

Brian Moon

20 years ago
> Indeed, especially as everyone will tell you that isset and empty are > equivalent.
Are you saying that people misunderstand that or that you believe that to be true? I see that misunderstanding a lot. I have to educate people on that. They don't seem to grok the subtle, yet crucial (and I believe, wonderful) difference between the two. Brian.

Pierre Joye

20 years ago
On 4/17/06, Brian Moon <brianm@dealnews.com> wrote:
> > Indeed, especially as everyone will tell you that isset and empty are > > equivalent. > > > Are you saying that people misunderstand that or that you believe that > to be true? I see that misunderstanding a lot. I have to educate > people on that. They don't seem to grok the subtle, yet crucial (and I > believe, wonderful) difference between the two.
I was not clear :) isset and empty share the same implementation, the only difference is what they return (in short). They behave "the same", they should continue do so if empty accept many arguments. But this discussion just hence Derick's point, it is not that obvious to bring consistency between the two functions. --Pierre

Richard Lynch

20 years ago
On Sun, April 16, 2006 7:38 pm, Pierre wrote:
> isset and empty share the same implementation, the only difference is > what they return (in short). They behave "the same", they should > continue do so if empty accept many arguments.
Actually... Unless the docs are lying to me... empty() checks the contents of the value, and does something quite different based on the value found. isset() just plain checks in the hash table[s] if the variable has been assigned, and that's it. Plus, the meaning of empty() changed in some a way with "0" between versions 3 and 4, and then again with respect to objects with no properties between 4 and 5. isset() has never changed its meaning out from under me. :-) So, while the guts of the function may be the same in source, there's got to be some kind of flag or something going on for empty() to be checking all those values, no???
-- Like Music? http://l-i-e.com/artists.htm

Unnamed Person

20 years ago
What it comes down to, is it runs the exact same code on the variables, I just changed the method of which they were called (ie: allowed multiple.) On 4/16/06, Richard Lynch <ceo@l-i-e.com> wrote:
> > On Sun, April 16, 2006 7:38 pm, Pierre wrote: > > isset and empty share the same implementation, the only difference is > > what they return (in short). They behave "the same", they should > > continue do so if empty accept many arguments. > > Actually... > > Unless the docs are lying to me... > > empty() checks the contents of the value, and does something quite > different based on the value found. > > isset() just plain checks in the hash table[s] if the variable has > been assigned, and that's it. > > Plus, the meaning of empty() changed in some a way with "0" between > versions 3 and 4, and then again with respect to objects with no > properties between 4 and 5. > > isset() has never changed its meaning out from under me. :-) > > So, while the guts of the function may be the same in source, there's > got to be some kind of flag or something going on for empty() to be > checking all those values, no??? > > -- > Like Music? > http://l-i-e.com/artists.htm > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
-- ------------------------------------ Graham Christensen www.itrebal.com www.iamgraham.net

Ron Korving

20 years ago
isset() does more than check the existance in a hash table, because this the following is true: $foo = null; isset($foo); // returns false, even though $foo is initialized echo $foo; // will not cause a NOTICE, because $foo is initialized - Ron ""Richard Lynch"" <ceo@l-i-e.com> wrote in message news:3516.209.254.223.2.1145240948.squirrel@www.l-i-e.com...

Unnamed Person

20 years ago
Should I go ahead and submit this patch? Where should I go about doing so? I looked around bugs.php.net but am unsure. On 4/17/06, Ron Korving <r.korving@xit.nl> wrote:
> > isset() does more than check the existance in a hash table, because this > the > following is true: > > $foo = null; > isset($foo); // returns false, even though $foo is initialized > echo $foo; // will not cause a NOTICE, because $foo is initialized > > - Ron > > > ""Richard Lynch"" <ceo@l-i-e.com> wrote in message > news:3516.209.254.223.2.1145240948.squirrel@www.l-i-e.com... > > On Sun, April 16, 2006 7:38 pm, Pierre wrote: > > > isset and empty share the same implementation, the only difference is > > > what they return (in short). They behave "the same", they should > > > continue do so if empty accept many arguments. > > > > Actually... > > > > Unless the docs are lying to me... > > > > empty() checks the contents of the value, and does something quite > > different based on the value found. > > > > isset() just plain checks in the hash table[s] if the variable has > > been assigned, and that's it. > > > > Plus, the meaning of empty() changed in some a way with "0" between > > versions 3 and 4, and then again with respect to objects with no > > properties between 4 and 5. > > > > isset() has never changed its meaning out from under me. :-) > > > > So, while the guts of the function may be the same in source, there's > > got to be some kind of flag or something going on for empty() to be > > checking all those values, no??? > > > > -- > > Like Music? > > http://l-i-e.com/artists.htm > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
-- ------------------------------------ Graham Christensen www.itrebal.com www.iamgraham.net

Andi Gutmans

20 years ago
Nope you shouldn't, as per my previous email. At 10:46 AM 4/17/2006, itrebal@gmail.com wrote:

Richard Lynch

20 years ago
On Sun, April 16, 2006 4:40 pm, Derick Rethans wrote:
> On Sat, 15 Apr 2006 itrebal@gmail.com wrote: > There is a thought about it, and that is that we could not decide > whether it should be an AND or an OR test between the different > parameters.
I'm curious to know whoulda thunk it would be OR, and why?... I mean, the most common usage I've seen, bar far, is for form input validation of a zillion inputs. I've never quite grokked why others think empty is "better" than isset for that, and don't really want to even get into that thread (again) here, but I am interested to know when you'd have a whole BUNCH of args to test with empty and would want OR on a regular frequent basis.
-- Like Music? http://l-i-e.com/artists.htm

Andi Gutmans

20 years ago
We had a huge thread about this a while back (1-2 years) where this was discussed in great depth. The agreement was that for isset() it's very straight forward and usefull and that the semantics with empty() would be confusing and problematic, and therefore, the decision was to just support this for isset(). For the full reasoning I suggest to search the archives. Shouldn't take too long to find. Andi At 04:24 PM 4/16/2006, Richard Lynch wrote:

Unnamed Person

20 years ago
When I'm validating form data, there are many occasions (more than not) that I require most of them to be completed, and not left blank. In which case you would do: if(empty($_GET['var1'], $_GET['var2'], $_GET['var3'])){ //Display error, yadda yadda } as aposed to if(empty($_GET['var1']) || empty($_GET['var2']) || empty($_GET['var3'])){ //Display error, yadda yadda } which is a much cleaner and simpler method. Doing it the other way (AND/OR) doesn't seem to have many applications that I can see. On 4/15/06, itrebal@gmail.com <itrebal@gmail.com> wrote:
> > The following is a direct excerpt from the PHP manual on empty, and isset: > bool *empty* ( mixed var ) > bool *isset* ( mixed var [, mixed var [, ...]] ) > Is there a reason empty does not allow multiple variables at a time, as > isset? Was there thought behind it, or is it just an inconsistency? >
-- ------------------------------------ Graham Christensen www.itrebal.com www.iamgraham.net