Coldfusion 8: IsDefined('URL.variable') and is not ""?
Asked Answered
S

5

6

I'm trying to find out if a url variable exists, and if it doesn't, make sure that it's not empty.

This does not work:

<cfif IsDefined('URL.affiliateId') and is not "">
    //
</cfif>
Sportsman answered 9/11, 2010 at 17:38 Comment(2)
All answers below work, but FYI structKeyExists() is more efficient then isDefined(), and len() is more efficient then NEQ "". Trim() is optional but not a bad idea to include that.Allpurpose
I think a better reason to use structKeyExists() is accuracy. IsDefined() has a slightly broader scope, which may occasionally lead to unexpected results.Postmeridian
R
15
<cfif structKeyExists(url, 'affiliateID') and trim(url.affiliateID) neq "">...</cfif>
Recovery answered 9/11, 2010 at 18:5 Comment(3)
Ah, much better. (I am always amazed how many ways you can write this kind of thing ;-)Postmeridian
Also it's been proven the sturctKeyExists is more efficient than IsDefined.Ketose
Just so you know why StructKeyExists is more efficient; StructKeyExists() looks for a single variable in a single structure. isDefined() will scan every scope in a prescribed order until it find the variable, even if you specify the scope for the variable in the function call. eg. isDefined("session.userid") will still scan the variables, URL, Form etc scopes until it finds it in the session scope.Hideandseek
S
4

You can simplify the logic a bit as well by using <cfparam> to ensure that the URL variable always exists. Then rather than having 2 conditions, you just need 1.

<cfparam name="URL.affiliateId" type="string" default="" />

<cfif trim( URL.affiliateId ) is not "">
     do stuff here
</cfif>
Spline answered 9/11, 2010 at 18:18 Comment(1)
True. If the variable's existence does not negatively effect existing code (I was not sure), then cfparam can simplify thingsPostmeridian
P
1

To ignore most white space

<cfif IsDefined('URL.affiliateId') and len(trim(URL.affiliateId))>
    value is defined and not empty
</cfif>

... or alternately

<cfif IsDefined('URL.affiliateId') and len(trim(URL.affiliateId)) gt 0>
    value is defined and not empty
</cfif>
Postmeridian answered 9/11, 2010 at 18:6 Comment(0)
A
0
<cfif IsDefined('URL.affiliateId') and URL.affiliateId neq "">
    //
</cfif>
Atlantes answered 9/11, 2010 at 17:47 Comment(4)
As Leigh says: no #'s are requires, plus if you know the scope and variable name you should always use structkeyexists() as shown by scrittlerHideandseek
yeah I know that #s are required, it's just one of those habits.Atlantes
Good point about structKeyExists(). Either works, but it is a bit more precise.Postmeridian
I think they have a CF support group for that .. ;)Postmeridian
G
0

I will just sum up the answers and offer my version of it:

<cfparam name="URL.affiliateId" type="string" default="" />

<cfif len(trim(URL.affiliateId))>
     ...do something with the affiliate...
</cfif>

You do not need structKeyExists or isDefined and it would be better to avoid them. Also you do not need the 'greater than zero' part after the 'len()'.

Gribble answered 10/11, 2010 at 11:4 Comment(3)
No there are perfectly valid use cases for all of the above. Personal preference, on the other hand, is a different matter.Postmeridian
Yes, they are valid but not needed.Jennelljenner
While there are similarities in behavior, there are also differences. So a blanket statement that they are never needed is inaccurate. They do not all provide identical functionality.Postmeridian

© 2022 - 2024 — McMap. All rights reserved.