ColdFusion expressions consist of operands and operators. Operands are comprised of constants and variables. Operators, such as the multiplication symbol, are the verbs that act on the operands; functions are a form of operator.
The simplest expression consists of a single operand with no operators. Complex expressions have multiple operators and operands. The following are all ColdFusion Expressions:
12
MyVariable (1 + 1)/2 "father" & "Mother" Form.divisor/Form.dividend Round(3.14159)
Operators act on the operands. Some operators, such as functions with a single argument, take a single operand. Many operators, including most arithmetic and logical operators, take two operands. The following is the general form of a two-operand expression:
Expression Operator Expression
Note that the operator is surrounded by expressions. Each expression can be a simple operand (variable or constant) or a subexpression consisting of more operators and expressions. Complex expressions are built up using subexpressions. For example, in the expression (1 + 1)/2, 1 + 1 is a subexpression consisting of an operator and two operands.
ColdFusion has four types of operators:
Functions also can be viewed as operators because they act on operands.
The following table describes the arithmetic operators:
Boolean, or logical, operators perform logical connective and negation operations. The operands of Boolean operators are Boolean (True/False) values.The following table describes the Boolean operators:
ColdFusion's decision, or comparison, operators produce a Boolean True/False result. The following table describes the decision operators:
You can replace some decision operators with alternative notations to make your CFML more compact, as shown in the following table:.
Operator |
Alternative name(s) |
---|---|
IS |
EQUAL, EQ |
IS NOT |
NOT EQUAL, NEQ |
GREATER THAN |
GT |
LESS THAN |
LT |
GREATER THAN OR EQUAL TO |
GTE, GE |
LESS THAN OR EQUAL TO |
LTE, LE |
The following rules apply to decision operators:
123.45 CONTAINS 3.4
"a" IS "A"
"ab" LT "aba" "abde" LT "ac"
There is one string operator, which is the concatenation operator.
Operator |
Description |
---|---|
& |
Concatenates strings. |
The order of precedence controls the order in which operators in an expression are evaluated. The order of precedence is as follows:
Unary +, Unary -
^ *, / \ MOD +, - & EQ, NEQ, LT, LTE, GT, GTE, CONTAINS, DOES NOT CONTAIN NOT AND OR XOR EQV IMP
To enforce a non-standard order of evaluation, you must parenthesize expressions. For example:
You can nest parenthesized expressions. When in doubt about the order in which operators in an expression will be evaluated, use parentheses to force the order of evaluation.
Functions are a form of operator. Because ColdFusion functions return values, you can use function results as operands. Function arguments are expressions. For example, the following are valid expressions:
The following table shows function syntax and usage guidelines:
All functions return values. In the following example, the cfset
tag sets a variable to the value returned by the Now
function:
<cfset myDate = DateFormat(Now(), "mmmm d, yyyy")>
You can use the values returned by functions directly to create more complex expressions, as in the following example:
Abs(Myvar)/Round(3.14159)
For more information on how to insert functions in expressions, see "Using pound signs".
Some functions take optional arguments after their required arguments. If omitted, all optional arguments default to a predefined value. For example:
The difference in the results is because the Replace
function takes an optional fourth argument that specifies the scope of replacement. The default value is "One," which explains why only the first occurrence of "Eat" was replaced with "Drink" in the first example. In the second example, a fourth argument causes the function to replace all occurrences of "Eat" with "Drink".
It is important to remember that ColdFusion evaluates function attributes as expressions before it executes the function. As a result, you can use any ColdFusion expression as a function attribute. For example, consider the following lines:
<cfset firstVariable = "we all need">
<cfset myStringVar = UCase(firstVariable & " more sleep!")>
When ColdFusion Server executes the second line, it does the following:
UCase
function.
UCase
function on the string argument "we all need more sleep!" to get "WE ALL NEED MORE SLEEP!".
ColdFusion completes steps 1-3 before invoking the function.