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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if's, any other way? 1

Status
Not open for further replies.

DeZiner

Programmer
May 17, 2001
815
0
0
US
I need to check for the existence and if defined the value.
This works but is there a better way?
<cfif isDefined('bnameFlag')>
<cfif bnameFlag EQ &quot;True&quot;>


I had this and it didn't work, it always eveluated to true even when truly it was false (the value not the defined.)
<cfif isDefined('bnameFlag') EQ &quot;True&quot;>
DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
Try this - it works on ColdFusion 5 anyway.

<cfset myDefinedVar = &quot;Test&quot;>
<cfoutput>
<cfif isdefined(&quot;myDefinedVar&quot;) IS &quot;True&quot;>
myDefinedVar is True!<br><br>
</cfif>
<cfif isdefined(&quot;myUNDefinedVar&quot;) IS &quot;False&quot;>
myUNDefinedVar is False!
</cfif>
</cfoutput>
 
Do you have to set it to the variable first, or can I just do like so:
<cfif isdefined(&quot;form.Var&quot;) IS &quot;True&quot;>
myDefinedVar is True!<br><br>
</cfif>
????

Thanks!
DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
IsDefined is apparently a slow function -- coldfusion experts (among which i do not count myself) recommend CFPARAM tag instead

<CFPARAM NAME=&quot;scope.myvar&quot; DEFAULT=&quot;$none&quot;>
<CFIF scope.myvar IS &quot;$none&quot;>
myvar was not defined
<CFELSE>
myvar was defined
value was <CFOUTPUT>#scope.myvar#</CFOUTPUT>
</CFIF>


the reason i like to scope this type of variable is because sometimes the app will take slightly different behaviour when the variable is passed in via the URL versus via a form field

substitute your own nonsense value for $none

rudy
 
i just do this, i like it cause its shorthand.

<cfif len(form.Var)>
if there is anything in there, it evaulates true and will display this
</cfif>

 
I have read the same thing Rudy did... IsDefined is slow.
And you should use CFPARAM when possible.

Though, I've also read that string compares are slower than length counts, so I'd revise his solution to something like:

Code:
<CFPARAM NAME=&quot;scope.myvar&quot; DEFAULT=&quot;&quot;>
<CFIF len(scope.myvar) GT 0>
  myvar was defined 
  value was <CFOUTPUT>#scope.myvar#</CFOUTPUT>
<CFELSE>
  myvar was not defined 
</CFIF>

or, as mcsolas points out, striking the GT 0 is shorthand... though I prefer to keep it in for readibility (I hate shorthand in code).


That being said, to answer your original question, if using CFPARAM is not a possibility in your case (and I'm not sure when it wouldn't be, but...)

You can use a simple AND in the CFIF. Statements are evaluated FIFO, left to right, unless you've used some special grouping.

So
Code:
<cfif isDefined('bnameFlag') AND bnameFlag EQ &quot;True&quot;>
would work. The isDefined is evaluated first. If it fails, no attempt is made to evaluate the second statement... so you won't get an error if it's not defined.

CFIF will accept AND's and OR's. And you can group statements as if they were algebreic equations (ie - with parenthesis).

Code:
<CFIF isDefined('bnameFlag') AND (bnameFlag EQ &quot;True&quot; OR bnameFlag EQ &quot;Yes&quot;)>

Hope it helps,
-Carl
 

star for the well-written explanation

&quot;i hate shorthand in code&quot; almost worth another!


rudy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top