On 31 Oct 2005, at 21:11, Chris Shiflett wrote:
I think this is where some (most?) of the misunderstanding
originates. Testing to see whether something exists is exactly what
isset() does.
No. Given $myarray['a'] = NULL, isset($myarray[a']) returns false.
You're saying that therefore, $myarray['a'] does not exist, which is
clearly untrue. Testing to see whether something has a value is
exactly what isset does, not whether it exists. In many cases it
comes down to the same thing (because if it doesn't exist it can't
have a value), but they are definitely not identical.
Wouldn't it seem like a bug if a function intended to check whether
a variable is set generates a notice when you test a variable that
isn't?
Since we're on such thin ice, you need to tread carefully - first you
said it's for "testing to see whether something exists", but now you
describe it as "a function intended to check whether a variable is
set". Which is it? They are not the same thing. Here's an experiment
to distinguish which it is - it may come as quite a surprise:
var_dump(isset($bar));
echo $bar;
$bar = NULL;
var_dump(isset($bar));
echo $bar;
bool(false)
PHP Notice: Undefined variable: bar in /test.php on line 3
Notice: Undefined variable: bar in /test.php on line 3
bool(false)
Holy Smoke! isset quite definitely CANNOT tell you if a variable does
not exist - though PHP clearly knows as the second echo did not
generate an error! If it returns true then it does definitely both
exist and has a value (which is useful and why you can use isset as
you do), but if it returns false you really have no idea if the var
exists or not. So what can you use instead? Is there no direct
equivalent of array_key_exists for variables? I did discover that
array_key_exists('bar', get_defined_vars()) works, but though at
least it's definitive, I don't think I could face using that
everywhere ;^)
To summarize, I see nothing wrong with your way of using
array_key_exists(), but I don't think you can claim Richard's use
of isset() is inappropriate. His method is "safer" in cases where
the array itself is not set, and your method is "safer" when an
element's value is NULL. Neither of these cases ever exist with
$_GET, $_POST, etc., because they are always set, and they only
contain strings. Therefore, there's no debate to be won here. :-)
Of course isset has a valid place - array_key_exists cannot replicate
what it does (it doesn't know about values), so they can play
together nicely - for example I'd consider this pretty robust:
if (isset($_SESSION) and is_array($_SESSION) and array_key_exists
('myvar', $_SESSION)) ...
If you get past that you can be absolutely certain that $_SESSION
['myvar'] exists, regardless of its value, and there is no
opportunity to trigger an error, whereas if you said:
if (isset($_SESSION) and is_array($_SESSION) and isset($_SESSION
['myvar'])) ...
you could still be wrong. The merest possibility of being wrong is a
bad thing in code. Why not use marginally different syntax and be
absolutely sure?
Yesterday I encountered an error in a large commercial php script and
it turned out that it was looking in $_SERVER['SERVER_NAME'] which
was there but set to NULL for some reason, and their test with isset
was failing. So it's not just academic and I'm not making it up -
this problem does happen for real.
All this over such a little thing - imagine if we had a whole
language to worry about! Oh wait...
Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
marcus@xxxxxxxxxxxxxxxxxx | http://www.synchromedia.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php