ColdFusion is weakly typed, meaning that you do not have to specify the data type that a variable holds. Rather, ColdFusion determines the data type of a variable at the time the variable is used. A common way to set a variable is with the <cfset> tag.
In ColdFusion, variable names:
There are many different flavors of variables in ColdFusion:
These flavors of variables each have a different scope, which determines where and how long a variable exists. For example, variables specific to a user's session are of the Session scope. They exist for the duration of the session and are accessible on every page that is a part of the session. Variables specific to a page (i.e, Local variables) are of the Variables scope.
A prefix is used to specify the scope of a variable. The syntax is shown below.
The following table shows several variable scopes and their prefixes. As shown in the table, not all prefixes are required.
|Variables||VARIABLES||Defined and accessible on the current page only.|
|CGI||CGI||Accessible on any page. Contains server environment variables.|
|URL||URL||Accessible on the current page. Contains values passed in on the query string.|
|Form||FORM||Accessible on the current page. Contains values passed in through a form.|
|Cookie||COOKIE||Accessible on any page. Contains variables held in cookies on the client machine.|
|Client||CLIENT||Accessible on any page. Contains variables created using the Client prefix.|
|Arguments||ARGUMENTS||Defined and accessible within a user-defined function or a ColdFusion Component method.|
|Session||SESSION||Prefix Required. Accessible on any page to a single user for the duration of a client session.|
|Application||APPLICATION||Prefix Required. Accessible on any page to all users until the application is restarted.|
|Server||SERVER||Prefix Required. Accessible on any page that is delivered from specific server.|
Variables of different scopes can have the same names. For example, a local variable could have the same name as a form variable. If such a variable is referred to without a prefix, ColdFusion needs to know which variable to use. ColdFusion will search through the scopes in the following order:
In general, it is a good idea to include the prefix when referring to variables as this will help avoid confusion and could make the page process more quickly as ColdFusion will know exactly where to look for the variable.
A ColdFusion page is generally a mixture of CFML tags, HTML tags, and plain text. ColdFusion ignores any content that is not contained within a CFML tag. That is, content outside of CFML tags is returned as is to the browser.
The <cfoutput> tag is used to tell ColdFusion to attempt to evaluate any variable or expressions contained within pound signs (#). Let's take a look at an example. You guessed it - "Hello World".
<cfset VARIABLES.greeting="Hello"> <html> <head> <title>Hello World!</title> </head> <body> <cfoutput>#VARIABLES.greeting# World!</cfoutput> </body> </html>
The code isn't very exciting. In fact, ColdFusion doesn't buy us anything here as we could have just as easily output the code using HTML. There is nothing dynamic about the script. Let's try something a little more interesting.
A common way to pass values from the browser to the server is by appending them to the URL as follows:
The part of the url that follows the question mark is called the query string. One or more name-value pairs can be passed to the server in this way. Each name-value pair is separated by an ampersand (&). The processing page can read these name-value pairs and use them to determine its response.
The HTML page below shows an example of how these name-value pairs might be passed.
<html> <head> <title>Preferred Greeting</title> </head> <body> Do you prefer a formal greeting or an informal greeting? <ul> <li><a href="HelloHi.cfm?greet=Hello">Formal</a></li> <li><a href="HelloHi.cfm?greet=Hi">Informal</a></li> <li><a href="HelloHi.cfm?greet=Howdy">Friendly</a></li> </ul> </body> </html>
<html> <head> <title><cfoutput>#URL.greet#</cfoutput> World!</title> </head> <body> <cfoutput>#URL.greet# World!</cfoutput> </body> </html>
<cfoutput> tags tell ColdFusion to look for variables and expressions to evaluate. Within
<cfoutput> tags, any content not in pound signs (
#) is simply ignored. Another way to write this page would be to replace the two sets of
<cfoutput> tags with an open
<cfoutput> tag before the open
<html> tag and a close
</cfoutput> tag after the close
<cfoutput> tags, as a general rule, cannot be nested.
ColdFusion comments are similar to HTML comments except that they take three dashes instead of two.