Working with CFML expressions

Expressions are an important part of the ColdFusion language. Expressions are a collection of different elements, ColdFusion variables, functions, and operators. You can think of them as strings of text that consist of one or more of the following elements:

Many examples of expressions were shown in this chapter; for example:

When you build expressions in ColdFusion, you can include simple and complex elements; how you represent these elements determines how ColdFusion processes your program.

Building expressions

In ColdFusion, you build expressions as you need them. The expressions can include simple elements, such as the expressions shown previously, or they can include complex elements, such as arithmetic functions, strings, and decision operators. (You build some complex expressions in Part II of this book.)

As mentioned, it is important that elements are identified properly in your expression so ColdFusion processes them as expected, and you can avoid unnecessary errors. When writing expressions, consider the following coding practices:

Specifying a consistent character case

Because the ColdFusion server is case-insensitive, you can write expressions using all uppercase, all lowercase, or mixed case. However, for code readability and consistency, you should use the same character case in all your programs. If you write your programs using the same case rules, you might prevent errors from occurring when you combine CFML on a page with case-sensitive languages, such as JavaScript.

Specifying pound signs to denote functions or variables

In ColdFusion, you specify pounds signs to denote functions and variables within a string of text. You use pounds signs to show the results of the function or variable on the page. Pounds signs instruct the ColdFusion server to evaluate the function (or variable) between the pound signs and display the value. The value of the function (or variable) appears in the browser as a result.

The following list identifies some common ways to use pound signs:

For more information and examples on using pound signs in expressions, see Developing ColdFusion MX Applications with CFML.

Specifying quotation marks around values

When assigning literal values to variables, you must surround the literal value with double quotation marks or single quotation marks. ColdFusion interprets the content between the quotation marks as a literal value and assigns that value to the variable; for example:

<cfset my_first_name = "Kaleigh">
<cfset my_last_name = "Smith">
<cfset my_age = 5>

ColdFusion instantiates the variable my_first_name to the string literal Kaleigh. Further, Smith is assigned to the variable my_last_name and 5 is assigned to age.

When referencing a variable by its name, you do not surround the name with quotation marks; for example:

<cfset the_string = "My name is " & variables.my_first_name & 
" and my age is " & variables.my_age>

My name is is literal text and, you therefore, surround it with quotation marks. The variable references variables.my_first_name and variables.my_age are not surrounded by quotation marks. ColdFusion uses the values of the referenced variables (Kaleigh and 5, respectively) when assigning the value to the variable the_string.

To display quotation marks on a page as literal characters, you must double the quotation marks; for example:

<cfset mystring = "We all shouted ""Happy Birthday"" when he entered the room.">
<cfoutput>
#mystring#
</cfoutput>

The result is the following output:

We all shouted "Happy Birthday" when he entered the room.

Specifying operators in expressions

In ColdFusion, you use operators to test conditions; for example, you use the IS operator to test for equality. When using operators in expressions, you must only use supported logical operators that ColdFusion can interpret properly. For example, if you use the greater than operator (>)or the less than operator (<), ColdFusion interprets these operators as the start or end of a tag.

The following table lists the nonsupported logical operators and their equivalent ColdFusion operators:
Nonsupported logical operator
Equivalent ColdFusion decision operator

Description
=
IS, EQUAL, EQ
Tests for equality.
<
LT, LESS THAN
Tests for less than.
<=
LTE, LE,
LESS THAN OR EQUAL TO
Tests for less than or equal to.
>
GT
GREATER THAN
Tests for greater than.
>=
GTE,
GREATER THAN OR EQUAL TO
Tests for greater than or equal to
< >
IS NOT, NEQ,
NOT EQUAL
Tests for nonequality.

CONTAINS
Tests whether a value is contained within a second value.

DOES NOT CONTAIN
Tests whether a value is not contained within a second value.

Arithmetic operators

The following table lists the arithmetic operators that ColdFusion supports:
Operators
Description
+, -, *, /
The basic arithmetic operators: addition, subtraction, multiplication, and division. In the case of division, the right operand cannot be zero.
+, -
Unary arithmetic operators for setting the sign of a number either positive or negative (+ or -).
Mod
Returns the remainder (modulus) after a number is divided by a divisor. The result has the same sign as the divisor. The right operand cannot be zero; for example: 11 MOD 4 is 3.
\
Divides two integer values. Use the \ (trailing slash) to separate the integers. The right operand cannot be zero; for example: 9 \ 4 is 2.
^
Returns the result of a number raised to a power (exponent). Use the ^ (caret) to separate the number from the power. The left operand cannot be zero; for example: 2 ^ 3 is 8.

String operator

The following table describes the one ColdFusion string operator that is a concatenation operator:
Operator
Description
&
Concatenates strings.

Comments