Welcome to WebmasterWorld Guest from 54.225.22.139

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

About using isset()

I think I am all confused again...

     
12:28 am on Jan 7, 2006 (gmt 0)

New User

10+ Year Member

joined:Apr 18, 2005
posts:26
votes: 0


I think I got myself confused over this topic.........again

//from php.net site

$a = array ('test' => 1, 'hello' => NULL);

var_dump(isset($a['test'])); // TRUE
var_dump(isset($a['foo'])); // FALSE
var_dump(isset($a['hello'])); // FALSE

//my testing

if(isset($_POST['submitted'])) {
print "submitted is set";
}

<form action="<?=$PHP_SELF;?>" method="post">
<input type="hidden" name="submitted" value=NULL>
<p><input name="submit" type="submit" value="submit"></p>
</form>

Notice that I explicitly set "submitted" to NULL

I used the following code to print out all my $_POST variables to make sure that "submitted" is indeed set to NULL

foreach($_POST as $key => $value)
print $key."=>".$value;

This prints out submitted=>NULL, submit=>submit so now I know for sure that "submitted" is set to NULL and according to the php.net site, using isset on a variable that is explicitly set to NULL will be false. But why is if(isset($_POST['submitted'])) return true and prints "submitted is set"?

8:00 am on Jan 7, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 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?

dc

9:03 am on Jan 7, 2006 (gmt 0)

New User

10+ Year Member

joined:Apr 18, 2005
posts:26
votes: 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)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 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.

dc

4:27 pm on Jan 7, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 27, 2003
posts:156
votes: 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:

array(2) {
["submitted"]=>
string(4) "NULL"
["submit"]=>
string(6) "submit"
}

the PHP constant NULL is not a string, which you can see from this test:

php > $array = array(NULL, 'NULL');
php > var_dump($array);
array(2) {
[0]=>
NULL
[1]=>
string(4) "NULL"
}
php >
6:24 pm on Jan 7, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 8, 2004
posts:500
votes: 0


spot on :)
6:25 pm on Jan 7, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 8, 2004
posts:500
votes: 0


you could use!empty($var) (not empty)
11:44 pm on Jan 7, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 27, 2003
posts:156
votes: 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)

Full Member

10+ Year Member

joined:July 22, 2005
posts:202
votes: 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)

New User

10+ Year Member

joined:Apr 18, 2005
posts:26
votes: 0


Thank you everyone for your explanation, my confusion has been cleared up now!