|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Creating and Using Variables
|
|
|
|
There are many reasons why you create variables in a ColdFusion page. To create a variable, you name it and assign a value to it.
<CFSET FirstName="Jack">
In this simple example, you use the CFSET tag to initialize the value. The scope of this variable is local, or within the application page where it is created.
When creating an array to store information, for example, you also use the CFSET tag to define a variable that contains the array:
<CFSET myarray=ArrayNew(1)>
You then use CFSET to add data to array elements:
<CFSET myarray[1]="January">
<CFSET myarray[2]="February">
...
For information about creating arrays in ColdFusion, see Advanced ColdFusion Development.
Another example is a CGI variable, which can be referenced to provide information about a particular user. You could determine a user's browser type by using the following CGI variable:
<CFOUTPUT>
#CGI.HTTP_USER_AGENT#
</CFOUTPUT>
|
|
|
|
Using CFSET to create variables |
|
|
|
The CFSET tag supports the creation of variables and the manipulation of variable values. You can use CFSET anywhere in an application page. The variable that is created can be used anywhere in the page after the CFSET tag.
If no variable prefix is supplied, the variables you create with CFSET are local variables, meaning they are available only on the page where they were created and pages included in it.
The CFSET tag can use a static value, a dynamic parameter, or an expression to create the variable.
The following shows the syntax of the CFSET tag:
<CFSET VariableName = Value, Parameter, or Expression>
|
|
|
|
Example: Static values
|
|
|
To create a variable called UserName based on a static string, use this syntax:
<CFSET UserName="Joe Doe">
Use quotation marks when the variable's value is a text string.
To create a variable UserNumber with a static numeric value, use this syntax:
<CFSET UserNumber=26>
|
|
|
|
Note
|
|
|
ColdFusion variables are typeless, which means you do not have to identify the type of data they contain, such as text or numbers. For more information on typeless expressions, see the Functions and Expressions chapter in Advanced ColdFusion Development.
|
|
|
|
Example: Dynamic parameters
|
|
|
To create a variable CurrentUser_ID based on the column name User_ID in a database query (GetUserID), which returns a numeric value, use this syntax:
<CFSET CurrentUser_ID=GetUserID.User_ID>
To create a variable UserDescription with a string that combines a dynamic parameter and text, use this syntax:
<CFSET UserDescription="#UserName# is a wonderful person.">
|
|
|
|
Example: Expressions
|
|
|
You can also use CFSET to create variables based on expressions. To create a variable called TotalValue based on a mathematical expression, use this syntax:
<CFSET TotalValue=2 * (4 + 5)>
To create a variable that combines strings and expressions, use an ampersand (&) sign:
<CFSET Pay="John's take home pay is" & (TotalValue - 1000)>
For more information, see the Functions and Expressions chapter in the Advanced ColdFusion Development book.
|
|
|
|
Displaying variables in a page |
|
|
|
To display the variable that has been set for a specific user, enclose the variable to be evaluated inside CFOUTPUT tags, as in the following code:
<CFOUTPUT>
Your favorite color is #Client.FavoriteColor#.
</CFOUTPUT>
Inside CFOUTPUT tags, always enclose variable names in pound signs (#). This signals that the variable name needs to be evaluated as a dynamic parameter. This way, ColdFusion outputs the variable's value and not the variable name itself.
|
|
|
|
Testing for a variable's existence |
|
|
|
Before relying on a variable's existence in an application page, you can test to see if it exists using the IsDefined function. For example, the following code checks to see if a Form variable named Order_ID exists:
<CFIF Not IsDefined("FORM.Order_ID")>
<CFLOCATION URL="previous_page.cfm">
</CFIF>
See the CFML Language Reference for more information on the IsDefined function.
|
|
|
|
Troubleshooting
|
|
|
If you attempt to evaluate a variable that has not been defined, ColdFusion will not be able to process the page. To help diagnose such problems, use the interactive debugger in ColdFusion Studio or turn Debugging on in the ColdFusion Administrator. The Administrator debugging information shows which variables are being passed to your application pages.
See the Debugging and Troubleshooting chapter for information on catching and fixing errors.
|
|
|
  
|
|
|
AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.
|