A Web application page is different from a static Web page because it can publish data dynamically. This involves creating, manipulating, and outputting variables.
A variable stores data that you can use in applications. As with other programming languages, you set variables in ColdFusion to store data that you want to access later. You reference a range of variables to perform different types of application processing.
ColdFusion variable names are case-insensitive. The variable names CITY and city refer to the same data.
The kind of information that variables contain varies. Two characteristics distinguish the information in a variable:
A variable's data type specifies the kind of value a variable can represent, such as a text string or number. ColdFusion does not require you to specify a variable's data type. Whether a variable represents a string, a number, a Boolean value (Yes/No), a date and time, or a more complex object such as an array or structure, ColdFusion automatically uses the appropriate internal data representation when you assign its value. However, ColdFusion does provide methods to examine and change the type of data that a variable represents. For a complete list of data types see the CFML Reference.
For example, use the following syntax to create a string variable:
<cfset mystring="Hello world">
The following example uses scientific notation to create a floating-point numeric variable:
<cfset myfloat=1.296e-3>
Variables differ in the source the data came from, the places in your code where they are meaningful, and how long their values persist. These considerations are generally referred to as a variable's scope.
ColdFusion has many different scope types, which are identified by prefixes to a variable name. For example, the variable Department in calldept.cfm is a local variable (a variable that has meaning on the current page). Local variables have the optional prefix Variables. Instead of writing:
I'd like to talk to someone in #Department#.
I'd like to talk to someone in #Variables.Department#.
Some variable scopes, such as the local scope, do not require the scope identifier prefix, while others do. However, it is good programming practice to use prefixes for most or all scopes. This helps to better identify each variable's use and can prevent multiple uses of the same name. This book uses the scope prefix for all variables except for local variables.
The following table lists some of the more common types of variable scopes and the prefixes that you use to identify the variables. Other chapters in this book discuss additional scope types. The CFML Reference has a complete list of scope types, their identifiers, and how they are used.
You surround a ColdFusion variable or function with pound signs (#) to tell the ColdFusion Server that it is not plain text. You only need to use pound signs in limited circumstances, particularly in the cfoutput and
cfquery
tag blocks. You do not need to use pound signs when you create a variable, assign it a value, or use it in a ColdFusion expression or as a parameter in a ColdFusion function.
Note Remember that ColdFusion cannot interpret anything, including variables, that is not inside a ColdFusion tag or tag block. |
The following table illustrates the basic use of pound signs. For a detailed description of the use of pound signs, see CFML Reference.
Applications can use many different variables. For example, the calldept.cfm
application page can set and display values for department, city, and salary.
<html> <head> <title>Call Department</title><br> </head> <body> <strong>Call Department</strong><br> <!--- Set all variables ---> <cfset Department="Sales"> <cfset City="Boston"> <cfset Salary="110000"> <!--- Display results ---> <cfoutput> I'd like to talk to someone in #Department# in #City# who earns at least #Salary#. </cfoutput> </body> </html>
http://127.0.0.1/myapps/calldept.cfm.