Re[2]: Re[2]: The 'at' sign (@) variable prefix

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Greetings, Maciek Sokolewicz.
In reply to Your message dated Thursday, October 9, 2008, 20:25:42,

>>>>> 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

Like to see how people calling me... somewhat... when I'm right :)
That just making my day every now and then.

>> <?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. Let's just ignore the fact that 
> isset is about a dozen times faster than array_key_exists.
> 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.

That's not an argument in the matter of code readability, portability and
understandability.
"Better be safe than sorry" (c) folk

When you want to check if array key has meaningful value or not, disregarding
it's content and/or presence, use isset(). But that have never happened to me
in, like, 10 years. And knowing if key is present critical for foreach() cycle.

On the other side, for every my project bigger than single page I always use
purifying function to cleanup/normalize _GET/_POST/_REQUEST.
And for every project I always have list of used user variables and it's
expected types/values. If variable have been passed, required, but contains
unreliable value, I could change it to NULL (which is, as you mentioned,
cannot be set by user) to indicate that it was passed to script AND purged
in validation process. (Have never used it but having such ability is for good)


-- 
Sincerely Yours, ANR Daemon <anrdaemon@xxxxxxxxxxx>


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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux