Page 1 of 1

Empty or Not?

Posted: Fri Oct 07, 2005 5:17 pm
by blacksnday
how can you have a variable/arg/etc that will be only used
if it has something defined for it?

Code: Select all

if($var1 == $var2)
{
      echo "something";
  }else{
      echo "something else";
}
Where I would like the above IF ELSE to be used ONLY if
$var2 has something inside it.

Re: Empty or Not?

Posted: Fri Oct 07, 2005 5:23 pm
by Nathaniel

Code: Select all

if ( !empty($var2) )
{
     if ( $var1 == $var2 )
     {
          //do something
     }
     else
     {
          //do something else
     }
}
;)

Posted: Fri Oct 07, 2005 5:25 pm
by blacksnday
doh! now that i look at that..... i slap myself !

thanks :)

Posted: Fri Oct 07, 2005 5:33 pm
by Nathaniel
No problem. ;)

Posted: Sat Oct 08, 2005 12:38 am
by elecktricity
I think this may work as well

Code: Select all

<?PHP
if($var1 == '' && $var1 == '')
{
//add stuff here
}
else
{
//add stuff here
}
?>

Posted: Sat Oct 08, 2005 5:36 pm
by Skara
empty also checks to see if the variable has been set, though. "if ($var == '')" will throw an error if it hasn't.

Posted: Sat Oct 08, 2005 5:37 pm
by Charles256
i thought it threw a notice? oh wel.getting senile i guess..

Posted: Sat Oct 08, 2005 6:11 pm
by feyd
it is a notice..

Posted: Sat Oct 08, 2005 8:16 pm
by Skara
A notice is a type of error. notice, warning, fatal, etc. :P Notices just don't cause any real problems.