homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

About using isset()
I think I am all confused again...

 12:28 am on Jan 7, 2006 (gmt 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>

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)

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:
array(2) {
string(4) "NULL"
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) {
string(4) "NULL"
php >


 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!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved