--- Terence <[EMAIL PROTECTED]> wrote:
> Say you have a querystring - index.php?myname=joe
> 
> To determine whether myname has a value I have come to the following 
> conclusions / shortcomings when using one of the following:

There is an inherent problem in the varying ways that people interpret
"has a value."

> ISSET = as long as the variable myname exists it's set, but there's no 
> guarantee of a value

If it's set, it has a value. There's no guarantee as to what value, of
course, but that's not what isset() is meant to tell you. It only tells
you whether the variable is set.

To me, the only way for a variable to be defined and not have a value is
for it to be set to null:

$foo = null;

isset(null) returns false, so this follows what I would expect. I can see
a valid argument that isset() should return true for this case (since
you're setting the variable), but I don't agree with any of the arguments
that say isset() returns true when it should be false. Well, I also agree
with the way it handles null, but I understand the confusion with that
one.

> !EMPTY = if your name = 0 (zero) then it's considered empty, for all 
> else it seems to work

Yes, this one is a bit weird. For integers, it makes perfect sense. For
example:

$foo = 0;

Here, it is clear that I intend $foo to be an integer, so 0 is pretty much
what I would consider empty. However, if I have a string:

$foo = '0';

I may no longer consider 0 to be empty. Unfortunately, this is not what
empty() will indicate, so I can understand the complaints. This is just
one of those (rare, in my opinion) cases where a loosely-typed language
can be tricky.

> STRLEN = I have found this to be the only sure way to know.

It is best to decide what you really want to test for. "Whether something
has a value" is terribly ambiguous, and thus, there is no way to test for
this in a way that will satisfy everyone. It's far too open to
interpretation. Just imagine what your has_a_value() function might test
for (if you were to write one), and I think you'll begin to see how others
might disagree with your approach.

If you simply want your string to not be of zero length, strlen() is a
fine test. You might want to throw a trim() in there if you don't consider
whitespace by itself to count.

Lastly, I think you'll find this to be a good reference:

http://www.blueshoes.org/en/developer/php_cheat_sheet/

Hope that helps.

Chris

=====
Chris Shiflett - http://shiflett.org/

PHP Security - O'Reilly
     Coming Fall 2004
HTTP Developer's Handbook - Sams
     http://httphandbook.org/
PHP Community Site
     http://phpcommunity.org/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to