Working with Variables

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.

About variables

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:

Data types

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>

Scope types

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#.

you can write:

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.
Scope type
Prefix
Description
Local (or Variables)
Variables
Variables created using cfset or cfparam, with or without specifying the scope prefix. You must define the variable on the current page or a page you include using cfinclude.
Form
Form
Data entered in tags in an HTML form or ColdFusion cfform tag block and processed on an action page.
URL
URL
Variables passed to a page as URL query string parameters.

Using the pound sign (#)

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.
CFML code
Results

cfset Department="Sales"> 

The variable named Department is created and the value is set to Sales.

<cfoutput> 

I'd like to talk to someone in Department. 

</cfoutput> 

ColdFusion does not treat Department as a variable because it is not surrounded by pound signs. The HTML page displays:
I'd like to talk to someone in Department.

<cfoutput> 

I'd like to talk to someone in #Department#. 

</cfoutput> 

ColdFusion replaces the variable Department with its value. The HTML page displays:
I'd like to talk to someone in Sales.

<cfoutput> 

The department name spelled backward is Reverse(Department). 

</cfoutput> 

ColdFusion sees Reverse(Department) as text and displays it unchanged. The HTML page displays:
The department name spelled backward is Reverse(Department).

<cfoutput> 

The department name spelled backward is #Reverse(Department)#. 

</cfoutput> 

ColdFusion uses the Reverse function to reverse the text in the Department variable and displays the result. The pound signs tell cfoutput to interpret Reverse as a ColdFusion function. The Reverse function uses the Department variable name. The HTML page displays:
The department name spelled backward is selaS.

Adding more variables to the application

Applications can use many different variables. For example, the calldept.cfm application page can set and display values for department, city, and salary.

To modify the application:

  1. Open the file calldept.cfm in ColdFusion Studio,.
  2. Modify the code so that it appears as follows:
    <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>
    
    
  3. Save the file.
  4. View the page in your Web browser by entering the following URL:
    http://127.0.0.1/myapps/calldept.cfm.
    
    


Banner.Novgorod.Ru