Empty or Not?

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

Post Reply
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Empty or Not?

Post 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.
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Re: Empty or Not?

Post by Nathaniel »

Code: Select all

if ( !empty($var2) )
{
     if ( $var1 == $var2 )
     {
          //do something
     }
     else
     {
          //do something else
     }
}
;)
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

doh! now that i look at that..... i slap myself !

thanks :)
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

No problem. ;)
User avatar
elecktricity
Forum Contributor
Posts: 128
Joined: Sun Sep 25, 2005 8:57 pm
Location: Trapped in my own little world.
Contact:

Post by elecktricity »

I think this may work as well

Code: Select all

<?PHP
if($var1 == '' && $var1 == '')
{
//add stuff here
}
else
{
//add stuff here
}
?>
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

empty also checks to see if the variable has been set, though. "if ($var == '')" will throw an error if it hasn't.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

i thought it threw a notice? oh wel.getting senile i guess..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it is a notice..
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

A notice is a type of error. notice, warning, fatal, etc. :P Notices just don't cause any real problems.
Post Reply