Re: Type of form element

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

 



Marcus Bointon wrote:
The thing I was wrong on is that PHP converts unset parameters (as
opposed to nonexistent ones which it obviously can't do anything
about) to an empty string, e.g. given ?a=&b=1, $_REQUEST ['a'] is
"", not NULL.

That's right, except we seem to have a vocabulary discrepancy:

1. To me, "unset" and "not set" are not the same thing (sort of like how "untie" and "not tied" are also not the same). I'm assuming you always mean "not set" in your comments. Otherwise, it sounds like you're describing some sort of conversion from one state to another. PHP is just taking what it is given.

2. Given the above assumption, there is no difference between something not being set and something not existing.

If you want to distinguish between $_GET['a'] and $_GET['b'] in your example above, empty() does the trick. Some people use strlen() if they want to also distinguish between the string 0 and the empty string, and you can always just compare something to the empty string.

However, it still serves to underline my other point that using
isset without actually knowing that is a potentially dangerous
thing.

I think Richard knew what he was doing. :-) If his approach is wrong, then so is mine, because I also use isset() to see whether a checkbox was checked. If you go back to my previous example, you'll see that array

Getting into the habit of using it for looking in the likes of
$_REQUEST means you're likely to use it other places where you
have no such guarantee, and you'll have a bug to track down.
Using array_key_exists means you will never be exposed to this
possibility, no matter where your data comes from.

I understand and appreciate your argument. However, you're now describing a social issue, not a technical one. These issues tend to be more subjective, so "right" answers are difficult to find. :-)

In this case, the only concern would be that PHP considers something set to NULL to be the same as not being set at all (as far as isset() is concerned). This situation can never exist with things like $_GET and $_POST, because everything in those arrays is a string.

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


[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