问题描述:

In ColdFusion, how can I determine if a variable exists within the querystring 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
相关阅读:
Top