Page 1 of 2

Why use isset()

Posted: Mon Aug 18, 2003 1:02 pm
by nigma
I see code snippets all the time with if loops like:

Code: Select all

if (isset($var))
{
  // do something
}
else
{
  // do something else
}
Isn't that the same as:

Code: Select all

if ($var)
{
  // do something
}
else
{
  // do something else
}

Posted: Mon Aug 18, 2003 1:31 pm
by McGruff
But what if:

Code: Select all

<?php
$var = 0;
$var = FALSE;
?>
I never use if($var), but always isset, is_null, or empty or some other test.

Posted: Mon Aug 18, 2003 1:32 pm
by nigma
0 and FALSE are not equivilant?

Posted: Mon Aug 18, 2003 1:34 pm
by JayBird
try these two example

Code: Select all

<?

$var = "";

if ($var) 
{ 
  print "the variable is set";
} 
else 
{ 
  print "the variable i not set";
}

?>
and this

Code: Select all

<?

$var = "";

if (isset($var)) 
{ 
  print "the variable is set";
} 
else 
{ 
  print "the variable i not set";
 }

?>
You'll see you get two different results. The first one equates to true becuase the variable is set to nothing, whereas the second one equates to false.

Hope you see the difference now

Posted: Mon Aug 18, 2003 1:35 pm
by nigma
Yes I do, thanks a bunch. So is empty() a more evolved isset()?

Posted: Mon Aug 18, 2003 1:36 pm
by McGruff
0 and FALSE are not equivilant?
Strictly speaking they're not: false is boolean type and 0 is integer.

However, the point I was trying to make was that with $var = 0/false, an isset test returns true (they are set) but an if($var) test returns false.

I don't think if($var) is good practice in general: better to be more specific.

Posted: Mon Aug 18, 2003 1:36 pm
by nigma
BTW McGruff, you notice that we registered for PHPDN within five days of each other?

Posted: Mon Aug 18, 2003 1:40 pm
by McGruff
I have a test.php page which I can't lay my hands on right now so I can't write up something with all the ins and outs.

Basically, the test file runs through all conceivable options for $var = 0, false, true, null, ''(empty string) and so on and outputs the result of isset, empty etc. I'd highly recommend you set something up like that to investigate - and the user comments in the php manual are also helpful.

Posted: Mon Aug 18, 2003 1:42 pm
by McGruff
nigma wrote:BTW McGruff, you notice that we registered for PHPDN within five days of each other?
Yeah - I'm not really a master - see signature. :)

Posted: Mon Aug 18, 2003 5:34 pm
by Gen-ik
To check if a value is truely FALSE you use three equal signs..

Code: Select all

<?php
if($var===false) { //the var is false }
if($var==false) { //var could be false or could be zero }
?>

Posted: Mon Aug 18, 2003 5:40 pm
by evilmonkey
Personally, I always use isset() when i want to check the whether a variable has a value. You never know when something might screw up, so it's better to have functioonal reassurance (isset() rather then if($var)). Although, the only time I ever use that function is to check a variable from the address bar. for example:

Code: Select all

$var=$_GET['somevar'];
if (isset($var)){
//do something
}
else {
//do something else
}
Cheers!

Posted: Mon Aug 18, 2003 10:18 pm
by BDKR
It's interesting, this thread. Recently on the PHP development list, someone proposed a function called variable_exists(). After that, an eruption of chatter occured about the vaugness of PHP and null verses 0 and all kinds of other stuff.

Anyways, as some have pointed out in a less direct manner, you can have an empty var. That said, be careful what it is you are testing for. Emptiness or existence? That said, just becuase it exists, don't assume it's not empty. My arse is full of scars behind (no pun intended) this issue. Sure, it's a subtle distinction, but an important one.

Cheers,
BDKR

Posted: Mon Aug 18, 2003 10:46 pm
by nielsene
evilmonkey wrote:

Code: Select all

$var=$_GET['somevar'];
if (isset($var)){
//do something
}
else {
//do something else
}
Using that code, you will always "do something" and never "do something else"
$var is always set (its value might be empty, but....)

You would want to do

Code: Select all

if (isset($_GET['somevar']))
i.e. you can't assign to a variable and expect it to be unset.....

Posted: Tue Aug 19, 2003 7:20 am
by Coco
aye or you would consider !empty() or !is_null() (tho empty returns true if the value is 0 OR null)

Posted: Tue Aug 19, 2003 7:48 am
by twigletmac
The main difference between

Code: Select all

if ($var) {
and

Code: Select all

if (isset($var)) {
is that the first will give you a warning (if, as you should have for development, your error reporting is at its highest level). Basically if you want to test whether a variable exists don't use the first method.

Mac