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>
Goyuix
  • 23,614
  • 14
  • 84
  • 128
dcolumbus
  • 9,596
  • 26
  • 100
  • 165
  • 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. – Henry Nov 09 '10 at 19:27
  • I think a better reason to use structKeyExists() is accuracy. IsDefined() has a slightly broader scope, which may occasionally lead to unexpected results. – Leigh Nov 09 '10 at 21:44

5 Answers5

15
<cfif structKeyExists(url, 'affiliateID') and trim(url.affiliateID) neq "">...</cfif>
scrittler
  • 1,121
  • 6
  • 8
  • Ah, much better. (I am always amazed how many ways you can write this kind of thing ;-) – Leigh Nov 09 '10 at 18:29
  • 2
    Also it's been proven the sturctKeyExists is more efficient than IsDefined. – Stefano D Nov 10 '10 at 19:32
  • 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. – Stephen Moretti Nov 11 '10 at 09:21
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>
charliegriefer
  • 3,342
  • 1
  • 18
  • 20
  • True. If the variable's existence does not negatively effect existing code (I was not sure), then cfparam can simplify things – Leigh Nov 09 '10 at 18:48
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>
Leigh
  • 28,765
  • 10
  • 55
  • 103
0
<cfif IsDefined('URL.affiliateId') and URL.affiliateId neq "">
    //
</cfif>
Harrison
  • 8,970
  • 1
  • 32
  • 28
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()'.

Uphill_ What '1
  • 683
  • 6
  • 15
  • 2
    No there are perfectly valid use cases for all of the above. Personal preference, on the other hand, is a different matter. – Leigh Nov 10 '10 at 12:21
  • Yes, they are valid but not needed. – Uphill_ What '1 Nov 11 '10 at 07:22
  • 1
    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. – Leigh Nov 11 '10 at 14:12