Marcus Bointon wrote:
But that's not the question you're using isset to answer. You just
want to know if something exists - you probably don't even care
what its value is.
I think this is where some (most?) of the misunderstanding originates.
Testing to see whether something exists is exactly what isset() does. I
don't find Richard's use of the function inappropriate.
I would also fully expect to receive an undefined index notice as
you have explicitly looked up an array index that does not exist.
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?
Your example seems no different than this:
$foo = isset($bar);
If $bar is not set, this code is still fine, as it should be.
$myarray = array();
print $myarray['a'];
This is not the same case, else I could rewrite my above example as:
echo $bar;
If $bar is not set, then this does generate a notice, as it should.
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. :-)
As I mentioned earlier, your argument about the social aspects of this
(such as the benefit of consistency gained by always using
array_key_exists()) is perfectly valid.
Hope that helps.
Chris
--
Chris Shiflett
Brain Bulb, The PHP Consultancy
http://brainbulb.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php