Variables in CFML; Assigning Data to Variables

To work with variables and the data they contain you must first be able to assign values to them.

Assigning Simple Values

Numeric and Boolean values can be assigned directly to variables. Here are some examples:

<cfset foo = 15>
<cfset foo = 1.6>
<cfset foo = True>
<cfset foo = False>

String values must be quoted to differentiate them from variable names:

<cfset foo = "My String">
<cfset foo = "My Other String">

Finally, values in variables can be assigned directly to other variables or the results of expressions as in the following examples:

<cfset fee = foo>
<cfset faa = Len(foo)>
<cfset foo = 5 + 3>

Make sure to read the section on Assignment by Value and Reference for important information regarding this.

Escaping Special Characters

Special characters, such as the pound sign ("#") and the double quote are used to distinguish variables from other CFML code. If you wish to use them as part of the variable data you must escape them. This tells ColdFusion to treat the special character(s) as part of the data, not part of the code.

Escaping in ColdFusion is done by doubling the character in question. So, if you wanted to create a variable with the value "#FFFFFF" (a hexidecimal number commonly used in web development) you would have to escape the pound sign like so:

<cfset foo = "##FFFFFF">

The same technique is used for double quotes as in the following:

<cfset foo = "He said ""I trust you"" but I didn't believe him.">

Pound Signs in Assignments

Pound signs ("#"), also sometimes called "Hash Marks" are used to indicate a CFML variable within a string (or, generally, within a CFOUTPUT). It's a common mistake to use pound signs more often than needed. The following example shows an example of how you'd use pound signs in an assignment.:

<cfset foo = "My String">
<cfset fee = "This is #foo#">

Pounds signs are only needed when you need to use a CFML variable within a string. In the following examples the pound signs are not required:

<cfset foo = #Len(Foo)#>
<cfset fee = #foo#>

Expressions

Any CFML expression can be used as the "value" for a variable. The variable will then contain the results of the expression. For example all of the following are legal variable assignments:

<cfset foo = 15>
<cfset faa = foo + 1>
<cfset fee = foo + faa>

The ampersand ("&") can used to concatenate strings instead of using pound signs to delimit them as in:

<cfset foo = "My String">
<cfset fee = "This is" & foo>

All CFML functions result in values that can be directly assigned to variables:

<cfset foo = now()>
<cfset faa = floor(15.7)>
<cfset fee = asc("A")>

Nulls (or the Lack of Them)

Unlike many languages CFML doesn't specifically contain the concept of null or "no value". Many languages that work well with ColdFusion such as SQL and JavaScript do contain the concept of null and because of this many beginning ColdFusion developer's become confused.

In CFML you can approach the concept somewhat poorly with the "empty string" as in:

<cfset foo = "">

The above code will create the variable "foo" but assign it no obvious value. This is not the same as "null" (which specifically indicates no value) but comes close enough for most purposes.

In most cases ColdFusion will convert nulls (as returned from a database call for example) to the empty string. However in some cases (as when you convert a query column to an array or to a list) ColdFusion simply eliminates null cells. You should be aware of this when working with nulls from external sources.

14 Current Sessions; Time: 04:30:17 07-01-2009; Tick: 578