开发者

In ColdFusion, how do I determine if a query string variable exists?

In ColdFusion, how can I determine if a variable exists within the querys开发者_开发知识库tring without throwing an error attempting to check it?


There are two options.

The first is to use cfparam to define a default value eg:

<cfparam name="url.varname" type="string" default="" />

This ensures that you can always refer to url.varname

The second is to use isDefined or structKeyExists to test for the presence of the variable:

<cfif isDefined("url.varname") and url.varname eq 42> do something </cfif>

or

<cfif structKeyExists(url, "varname") and url.varname eq 42> do something </cfif>


I have used this approach in many places.

At the top of the page:

<cfparam name="request.someVal" default="request.defaultVal">

Later in the page or custom tag, check for the value of the request.someVal variable, without fear of it crashing, since it has a default value.

<cfif ("request.someVal" eq "something")>
    ...
</cfif>
.
.
.


In <cfscript>, you can

param url.varname;  // throws error if it does not exist

param url.varname = ""; // sets value it was not already set
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜