Why use isset()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Why use isset()

Post 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
}
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Last edited by McGruff on Wed Aug 10, 2005 9:54 pm, edited 2 times in total.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

0 and FALSE are not equivilant?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Yes I do, thanks a bunch. So is empty() a more evolved isset()?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Last edited by McGruff on Mon Aug 18, 2003 1:43 pm, edited 1 time in total.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

BTW McGruff, you notice that we registered for PHPDN within five days of each other?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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. :)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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 }
?>
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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!
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.....
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

aye or you would consider !empty() or !is_null() (tho empty returns true if the value is 0 OR null)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply