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.
<cfset firstname = "Paul">
<cfset age = 63>
In ColdFusion, variable names:
- consist of letters, digits, underscores, and dollar signs
- cannot begin with a digit
- are case-insensitive
Variable Prefixes and Scope
There are many different flavors of variables in ColdFusion:
- variables specific to a page
- variables specific to a user's session
- variables specific to an entire application
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.
||Defined and accessible on the current page only.
||Accessible on any page. Contains server environment variables.
||Accessible on the current page. Contains values passed in on the query string.
||Accessible on the current page. Contains values passed in through a form.
||Accessible on any page. Contains variables held in cookies on the client machine.
||Accessible on any page. Contains variables created using the Client prefix.
||Defined and accessible within a user-defined function or a ColdFusion Component method.
||Prefix Required. Accessible on any page to a single user for the duration of a client session.
||Prefix Required. Accessible on any page to all users until the application is restarted.
||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:
- Local (Variables)
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".
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.
ColdFusion comments are similar to HTML comments except that they take three dashes instead of two.
<!-- This is an HTML comment -->
<!--- This is a ColdFusion comment --->