Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a difference for this booleans

Status
Not open for further replies.

r2d22y

Technical User
Nov 30, 2004
46
SE
Hello

Is there a difference between:

if(defined $myvalue){

}


and

if($myvalue){

}

What I want to control is if $myvalue has a value or not
$myvalue is a Hashvalue that maybe isnt set so thats wy I have to control if it exists.

Regards/D_S
 
Yes, there is a BIG difference.

Your second example tests for "truth". 0, "" and undef are all considered false and everything else is true.

In your first example, ONLY undef is false, everything else (including 0 and "") is true.



Trojan.
 
if in your code you have
Code:
$foo{key} = 0;
$myvalue = $foo{key};
if (defined $myvalue){print "defined\n";}
it will print 'defined '
Code:
$myvalue = 0;
if (defined $myvalue){print "defined\n";}
it will print 'defined '
Code:
$myvalue = $foo{key};
if (defined $myvalue){print "defined\n";}
it will print nothing

on the other hand
Code:
$myvalue = $foo{key};
if ($myvalue){print "it has something in it\n";}
it will print nothing

Code:
$myvalue = 0;
if ($myvalue){print "it has something in it\n";}
it will print nothing

Code:
$myvalue = "whatever";    # except 0 or ''
if ($myvalue){print "it has something in it\n";}
it will print "it has something in it"

Did you get the difference?


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top