| 8:00 am on Jan 7, 2006 (gmt 0)|
isset checks the existence of a variable, whether it be true or false the isset function will return true because the variable is set. In your case the variable is set to NULL, but its still set.
What are you trying to do exactly?
| 9:03 am on Jan 7, 2006 (gmt 0)|
I am just trying to understand isset better because somehow I still find it confusing.
You say isset will return true no matter what the value is then why would it be false in the following case?
$a = array ('test' => 1, 'hello' => NULL);
var_dump(isset($a['hello'])); // FALSE
| 10:14 am on Jan 7, 2006 (gmt 0)|
Well I assume that the var_dump function is clearing the variable completely and what its saying is 'if it is set clear it'. In this case the variable would then not exist and as such return false.
| 4:27 pm on Jan 7, 2006 (gmt 0)|
actually isset does return false for values set to the php constant NULL.
|If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant. |
However, you are not posting the php constant NULL, you are posting a string with the value of NULL.
You can dump your $_POST to see this. var_dump($_POST) on your form outputs:
the PHP constant NULL is not a string, which you can see from this test:
php > $array = array(NULL, 'NULL');
php > var_dump($array);
| 6:24 pm on Jan 7, 2006 (gmt 0)|
spot on :)
| 6:25 pm on Jan 7, 2006 (gmt 0)|
you could use!empty($var) (not empty)
| 11:44 pm on Jan 7, 2006 (gmt 0)|
>> you could use!empty($var) (not empty)
That would be my recommendation as well for checking if a POSTed variable has a value. Though there are a few cases where you will also need to use isset(), for example checkboxes which are not posted if they are not checked.
Some people also use isset() for other reasons on POST data as well. One example would be that it is good practice to use isset() if you are not sure if a variable is set or not, which in the case of POST data, it is subject to all kinds of manipulation, and you can't be sure of anything. So if you're doing that, you probably want to do a double check, "if is set and not empty".
| 11:56 pm on Jan 7, 2006 (gmt 0)|
Post data is transmitted as a string.
If you submit something like
<input type="hidden" name="number" value=34>
$_POST['number'] will have the string type
$is_int = is_int($_POST['number']); //$is_int set FALSE
$is_string = is_string($_POST['number']); //$is_string set TRUE
$is_numeric = is_numeric($_POST['number']); //$is_numeric set TRUE
| 6:17 pm on Jan 10, 2006 (gmt 0)|
Thank you everyone for your explanation, my confusion has been cleared up now!