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:
&
for joining statements, +
for addition, and so on)Many examples of expressions were shown in this chapter; for example:
#variables.my_full_name#
DateFormat(Now())
my_first_name= "Kaleigh"
When you build expressions in ColdFusion, you can include simple and complex elements; how you represent these elements determines how ColdFusion processes your program.
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:
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.
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:
<cfoutput> Hello #variables.my_first_name# </cfoutput>
If you omit the pound signs, the text, not the value, appears on the page.
cfset
to assign one variable's value to another value:<cfset my_full_name = variables.my_first_name & " " & variables.my_last_name>
<cfoutput> ##1: Your name. </cfoutput>
The result is the following output:
#1. Your name.
For more information and examples on using pound signs in expressions, see Developing ColdFusion MX Applications with CFML.
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.
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:
The following table lists the arithmetic operators that ColdFusion supports:
The following table describes the one ColdFusion string operator that is a concatenation operator:
Operator |
Description |
---|---|
& |
Concatenates strings. |