Setting Values
In ColdFusion, you create variables by giving them a name and assigning them a value. In this step, we will concentrate on the most commonly used ways to assign a value to a variable.
Using <CFSET>
As we have already seen, the <CFSET> tag can be used to assign a value to a variable. The <CFSET> tag is quick and easy to use and has no attributes other than the variable name and its assigned value. For example, the line of code
<CFSET Qty=2>
would create a variable named Qty and would set the value of that variable to the numerical value of 2. If the variable already exists, you can use <CFSET> to reassign the variable a different value. For example, the code
<CFSET Qty=2> <CFSET Qty=3> <CFOUTPUT>You have ordered #Qty# jars.</CFOUTPUT>
would first set the value of the Qty variable to 2. Because the Qty variable already exists, the second line of code would then reassign the variable's value to 3. The third line of code would then output You have ordered 3 jars.
You can also perform calculations inside the <CFSET> tag when assigning a value. For example, the code
<CFSET Qty=2> <CFSET Qty=Qty + 1> <CFOUTPUT>You have ordered #Qty# jars.</CFOUTPUT>
would produce the same result as the previous code. The second line would take the current value of the Qty variable (in this case 2), add 1 to it, and assign that value (3) back to the Qty variable. This is a common way to increment the value of a variable.
Using <CFPARAM>
Trying to use or output a variable that has not yet been created will cause an error. Figure 2.1 shows you the error output that ColdFusion will return if you try to use the following line of code without first creating the Qty variable.
<CFOUTPUT>You have ordered #Qty# jars.<CFOUTPUT>
Figure 2.1 The error message that results from failing to create variable.
Because we want to avoid errors, it is good practice to check whether a variable exists before you try to use it. Normally, the code would flow something like this.
Check to see if a variable currently exists.
If it does, use the variable's current value.
If the variable does not exist, assign it some default value.
Display or use the variable.
The <CFPARAM> tag encapsulates all this logic into one line of code. The <CFPARAM> tag takes three attributes, as outlined in Table 2.4.
Table 2.4 <CFPARAM> Tag Attributes
Attribute |
Description |
NAME |
The name of the variable to be checked, including (required) scope prefix. |
DEFAULT |
The default value you would like to assign to the variable (optional) if it does not already exist. |
TYPE |
The type of data that the variable should contain.*(optional) |
*(See the "Language Reference" section of http://www.LearnColdFusionMX.com for possible values.)
For example, in the code
<CFPARAM NAME="Qty" DEFAULT=1> <CFOUTPUT>Quantity ordered is #Qty#.</CFOUTPUT>
the first line would check for the existence of a variable named Qty. Because this variable has not yet been created, it will be created by the <CFPARAM> tag and will be assigned the default value of 1. The second line of code then outputs Quantity ordered is 1.
If a variable with the same name already exists, the <CFPARAM> tag is ignored. For example, in the code
<CFSET Qty=2> <CFPARAM NAME="Qty" DEFAULT=1> <CFOUTPUT>Quantity ordered is #Qty#.</CFOUTPUT>
the first line of code sets the value of the Qty variable to the numeric value of 2. On the second line, the <CFPARAM> tag checks for the existence of a variable named Qty. Because this variable already exists, the <CFPARAM> tag is ignored, and the program flow continues on to the third line, which outputs Quantity ordered is 2.
The <CFPARAM> tag is a quick and simple way to check for the existence of variables before you attempt to use them. It will come in very handy when passing variables from one page to the next, such as when processing form data.