Creating Default Variables with CFPARAM

Another way to create a variable is to test for its existence and optionally supply a default value if the variable does not already exist. The following shows the syntax of the CFPARAM tag:

<CFPARAM NAME="VariableName"
    TYPE="data_type"
    DEFAULT="DefaultValue">

There are two ways to use the CFPARAM tag, depending on how you want the validation test to proceed.

The following example shows how to use the CFPARAM tag to check for the existence of an optional variable and to set a default value if the variable does not already exist:

<CFPARAM NAME="Form.Contract" DEFAULT="Yes">

Example: Testing for variables

Using CFPARAM with the NAME variable is a way to clearly define the variables that a page or a custom tag expects to receive before processing can proceed. This can make your code more readable, as well as easier to maintain and to debug.

For example, the following series of CFPARAM tags indicates that this page expects two form variables named StartRow and RowsToFetch:

<CFPARAM NAME="Form.StartRow">
<CFPARAM NAME="Form.RowsToFetch">

If the page with these tags is called without either one of the form variables, an error occurs and the page stops processing.

Example: Setting default values

In this example, CFPARAM is used to see if optional variables exist. If they do exist, processing continues. If they do not exist, they are created and set to the DEFAULT value.

<CFPARAM NAME="Cookie.SearchString" DEFAULT="temple">

<CFPARAM NAME="Client.Color" DEFAULT="Grey">

<CFPARAM NAME="ShowExtraInfo" DEFAULT="No">

You can also use CFPARAM to set default values for URL and Form variables, instead of using conditional logic.