On Thu, Oct 9, 2008 at 12:25 PM, Maciek Sokolewicz <[EMAIL PROTECTED]> wrote:
> ANR Daemon wrote:
>>
>> Greetings, Ashley Sheridan.
>> In reply to Your message dated Wednesday, October 8, 2008, 23:08:37,
>>
>>>>> If you're using it to deal with possible empty input data, you'd better
>>>>> do it
>>>>> explicitly enstead.
>>>>>
>>>>> Something like this:
>>>>>
>>>>>  if(!array_key_exists('from_year', $_POST)
>>>>>   || !array_key_exists('from_month', $_POST)
>>>>>   || !array_key_exists('from_day', $_POST)
>>>>>   )
>>>>>  {
>>>>>   throw new Exception('No start date given', 100);
>>>>>  }
>>>>
>>>> *cough*
>>>>
>>>> filter_input does this elegantly too ;) as does an isset() on the array
>>>> index
>>>>
>>> I'm a fan of the isset() method for POST and GET variables, as usually
>>> I'll still want to put something in the variables I'm assigning those
>>> values to, rather than the NULL which gets returned by the @ prefix.
>>
>> Well, filter_input does not exist in 5.1.6, and iset() does not work
>> correctly
>> with array keys in general.
>
> bullshit

That's a bit harsh. Ash is right, and there are times when knowing
whether a key has a value -- even if that value is null -- is
important. In this case isset() may be adequate for the task, but it
does not always correctly tell you if an array key has been defined.

>> <?php
>>
>> $a = array ('test' => 1, 'hello' => NULL);
>>
>> var_dump(isset($a['test']));            // TRUE
>> var_dump(isset($a['foo']));             // FALSE
>> var_dump(isset($a['hello']));           // FALSE
>>
>> // The key 'hello' equals NULL so is considered unset
>> // If you want to check for NULL key values then try:
>> var_dump(array_key_exists('hello', $a)); // TRUE
>>
>> ?>
>>
>> (c) http://php.net/isset
>
> The only case in which isset() returns false even though it IS set is when
> the value is null. That is the _only_ difference in functionality between
> isset and array_key_exists.

In the case of $_GET or $_POST, it's not a huge deal, but it is a difference.

> Let's just ignore the fact that isset is about a
> dozen times faster than array_key_exists.

I'm not sure where you get that number. I have seen sites posted a
couple years ago talking about the difference, but I'm not sure it
applies to current versions. Granted, my test case is just timing a
basic loop since I don't have a profiler set up on this machine, but I
see no appreciable difference between the two. The time to execute
either function is somewhere on the order of 3.3E-005 on this machine
(which I know is slower than our web servers). I'm not usually calling
either function thousands of times to notice a difference in a script.


> But tell me, how often do you get a NULL value from $_GET or $_POST ?
> Because let me tell you, I don't see such a value...ever... And even if I
> did see it, it would not be a VALID value.

I agree you should never see NULL in either of those arrays (unless
you modify them in your code to put a null in one of them). However,
array_key_exists() appears to ALWAYS return the correct value whereas
isset() has one specific case where it is different. When you factor
that there appears to be negligible difference in performance between
the two, isset() is adequate but I see no reason to prefer it over
array_key_exists().


Andrew

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

Reply via email to