开发者

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

开发者 https://www.devze.com 2023-01-24 01:05 出处:网络
I\'m trying to find out if a url variable exists, and if it doesn\'t, make sure that it\'s not empty.

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 Is开发者_运维技巧Defined('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()'.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号