Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 67

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
The CFML Programming Language
To test whether an element exists in an array before you display its value, use a format such as the following:
</cfoutput>
<cfloop index="i" from="1" to="#Arraylen(myArray)#">
<cfif ArrayIsDefined(myArray, i)>
#i#: #myArray[i]#<br>
</cfif>
</cfloop>
</cfoutput>
Notice that in the
ArrayIsDefined
in quotation marks.
If you attempt to evaluate a variable that you did not define, ColdFusion cannot process the page and displays an error
message. To help diagnose such problems, turn on debugging in the ColdFusion Administrator or use the debugger
in your editor. The Administrator debugging information shows which variables are being passed to your application
pages.
Variable existence considerations
If a variable is part of a scope that is available as a structure, you might get a minor performance increase by testing the
variable's existence using the
You can also determine which Form variables exist by inspecting the contents of the
variable. This variable contains a list of all the fields submitted by the form. Remember, however, that form text fields
are always submitted to the action page, and can contain an empty string if the user did not enter data.
Using the cfparam tag
You can ensure that a variable exists by using the
supplies a default value if the variable does not exist. The
<cfparam name="VariableName"
type="data_type"
default="DefaultValue">
Note: For information on using the
There are two ways to use the
test to proceed:
• With only the
attribute to test that a required variable exists. If it does not exist, the ColdFusion server stops
name
processing the page and displays an error message.
• With the
and
name
default
processing continues and the value is not changed. If the variable does not exist, it is created and set to the value of
the
attribute, and processing continues.
default
The following example shows how to use the
a default value if the variable does not already exist:
<cfparam name="Form.Contract" default="Yes">
Example: testing for variables
Using the
tag with the
cfparam
expects to receive before processing can proceed. This can make your code more readable, as well as easier to maintain
and debug.
function, unlike the
IsDefined
function instead of the
StructKeyExists
cfparam
cfparam
attribute to validate the parameter data type, see the CFML Reference.
type
tag to test for variable existence, depending on how you want the validation
cfparam
attributes to test for the existence of an optional variable. If the variable exists,
tag to check for the existence of an optional variable and to set
cfparam
attribute is one way to clearly define the variables that a page or a custom tag
name
Last updated 1/20/2012
function, you do not surround the variable name
function.
IsDefined
Form.fieldnames
tag, which tests for the variable's existence and optionally
tag has the following syntax:
62
built-in

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents