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>
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>
<cfif structKeyExists(url, 'affiliateID') and trim(url.affiliateID) neq "">...</cfif>
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>
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>
<cfif IsDefined('URL.affiliateId') and URL.affiliateId neq "">
//
</cfif>
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()'.
© 2022 - 2024 — McMap. All rights reserved.