Forum Moderators: coopster

Message Too Old, No Replies

Difference between isset and ""

         

salewit

1:10 am on Aug 20, 2009 (gmt 0)

10+ Year Member



Simple question. What is the difference (if there is one) between:


if (isset($foo))

-AND-


if ($foo != "")

rocknbil

2:12 am on Aug 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"" is set, but it's a blank string.

This should clarify. Dump this in isset.php then call it directly, like example.com/isset.php.

<?php
if (isset($_GET['foo'])) { echo "Foo has been set <br>\n"; }
else { echo "no foo <br>\n"; }
echo "The variable type of foo is " . gettype($_GET['foo']) . "<br>\n";
echo "Now let's unset it:<br>\n";
unset($_GET['foo']);
if (isset($_GET['foo'])) { echo "Foo has been set <br>\n"; }
else { echo "no foo <br>\n"; }
?>

The gettype() will kick an error, but it tell us what you need to know - you should get


no foo
Notice: Undefined index: foo in /path/to/isset.php on line 4
The input value of foo is NULL
Now let's unset it:
no foo

Now call it with a query string, example.com/isset.php?foo=1


Foo has been set
The variable type of foo is string
Now let's unset it:
no foo

But wait! Try

example.com/isset.php?foo

Just like that without an equal sign after foo to set a value, not even ''.

Guess what you get? You might expect null, but no. It's an empty string.


Foo has been set
The variable type of foo is string
Now let's unset it:
no foo