Expressions

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.

Operator types

ColdFusion has four types of operators:

Functions also can be viewed as operators because they act on operands.

Arithmetic operators

The following table describes the arithmetic operators:
Operator
Description
+  -  *  /
Basic arithmetic: addition, subtraction, multiplication, and division. In division, the right operand cannot be zero.
+  -
Unary arithmetic: Set the sign of a number.
MOD
Modulus: Return the remainder after a number is divided by a divisor. The result has the same sign as the divisor. The right should be an integer; using an integer causes an error, and if you specify a real number ColdFusion ignores the fractional part; for example, 11 MOD 4 is 3.
\
Integer division: Divide an integer by another integer. Use the backslash character (\) to separate the integers. The right operand cannot be zero. For example, 9\4 is 2.
^
Exponentiation: Return the result of a number raised to a power (exponent). Use the caret character (^) to separate the number from the power; for example, 2^3 is 8. Real and negative numbers are allowed for both the base and the exponent. However, any expression that equates to an imaginary number, such -1^.5 results in the string "-1.#IND. ColdFusion does not support imaginary or complex numbers.

Boolean 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:
Operator
Description
NOT
Reverse the value of an argument. For example, NOT True is False and vice versa.
AND
Return True if both arguments are True; return False otherwise. For example, True AND True is True, but True AND False is False.
OR
Return True if any of the arguments is True; return False otherwise. For example, True OR False is True, but False OR False is False.
XOR
Exclusive or: Return True if one of the values is True and the other is False. Return False if both arguments are True or both are False. For example, True XOR True is False, but True XOR False is True.
EQV
Equivalence: Return True if both operands are True or both are False. The EQV operator is the opposite of the XOR operator. For example, True EQV True is True, but True EQV False is False.
IMP
Implication: The statement A IMP B is the equivalent of the logical statement "If A Then B." A IMP B is False only if A is True and B is False. It is True in all other cases.

Decision operators

ColdFusion's decision, or comparison, operators produce a Boolean True/False result. The following table describes the decision operators:
Operator
Description
IS
Perform a case-insensitive comparison of two values. Return True if the values are identical.
IS NOT
Opposite of IS. Perform a case-insensitive comparison of two values. Return True if the values are not identical.
CONTAINS
Return True if the value on the left is contained in the value on the right.
DOES NOT CONTAIN
Opposite of CONTAINS. Return True if the value on the left is not contained in the value on the right.
GREATER THAN
Return True if the value on the left is greater than the value on the right.
LESS THAN
Opposite of GREATER THAN. Return True if the value on the left is smaller than the value on the right.
GREATER THAN OR EQUAL TO
Return True if the value on the left is greater than or equal to the value on the right.
LESS THAN OR EQUAL TO
Return True if the value on the left is less than or equal to the value on the right.

Alternative notation for 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

Decision operator rules

The following rules apply to decision operators:

String operators

There is one string operator, which is the concatenation operator.
Operator
Description
&
Concatenates strings.

Operator precedence and evaluation ordering

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.

Using functions as operators

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:

Function syntax

The following table shows function syntax and usage guidelines:
Usage
Example
No arguments
Function()
Basic format
Function(Data)
Nested functions
Function1(Function2(Data))
Multiple arguments
Function(Data1, Data2, Data3)
String arguments
Function('This is a demo') 
Function("This is a demo")
Arguments that are expressions
Function1(X*Y, Function2("Text"))

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".

Optional function arguments

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".

Expression evaluation and functions

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:

  1. Determines that there is an expression with a string concatenation.
  2. Evaluates the firstVariable variable as the string "we all need".
  3. Concatenates "we all need" with the string " more sleep!" to get "we all need more sleep!".
  4. Passes the string "we all need more sleep!" to the UCase function.
  5. Executes the UCase function on the string argument "we all need more sleep!" to get "WE ALL NEED MORE SLEEP!".
  6. Assigns the string value "WE ALL NEED MORE SLEEP!" to the variable myStringVar.

ColdFusion completes steps 1-3 before invoking the function.

Comments