Expressions

ColdFusion expressions consist of operands and operators. Constants and variables are operands. Operators, such as the multiplication sign, 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 
a++ 
(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

Expressions surround the operator. 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 Five types of operators:

  • Arithmetic
  • Boolean
  • Decision (or comparison)
  • String
  • Ternary
    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.

++ –

Increment and decrement. Increase or decrease the variable by one.These operators can be used for pre-incrementing or decrementing (as in {{ x = ++ i}}), where the variable is changed before it is used in the expression. They can also be used for post-incrementing or decrementing (as in x = i+), where the value is changed after it is used in the expression. If the value of the variable i is initially 7, for example, the value of x in x = ++i is 8 after expression evaluation, but in x=i, the value of x is 7. In both cases, the value of i becomes 8.These operators cannot be used with expressions that involve functions, as in f().a. Also, you can use an expression such as -x, but ---x and +x cause errors, because their meanings are ambiguous. You can use parentheses to group the operators, as in -(--x) or (++x), however.

+= -= *= /= %=

Compound assignment operators. The variable on the right is used as both an element in the expression and the result variable. Thus, the expression a += b is equivalent to a = a +b.
An expression can have only one compound assignment operator.

+ -

Unary arithmetic: Set the sign of a number.

MOD or %

Modulus: Return the remainder after a number is divided by a divisor. The result has the same sign as the divisor. The value to the right of the operator should be an integer; using a non-numeric value causes an error, and if you specify a real number, ColdFusion ignores the fractional part (for example, 11 MOD 4.7 is 3).

 

Integer division: Divide an integer by another integer. The result is also an integer; for example, 9\4 is 2. The right operand cannot be zero.

^

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
or !

Reverse the value of an argument. For example, NOT True is False and the inverse.

AND
or &&

Return True if both arguments are True; return False otherwise. For example, True AND True is True, but True AND False is False.

OR
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

The ColdFusion decision, or comparison, operators produce a Boolean True/False result. Many types of operation have multiple equivalent operator forms. For example, IS and EQ perform the same operation. The following table describes the decision operators:

Operator

Description

IS EQUAL
EQ

Perform a case-insensitive comparison of two values. Return True if the values are identical.

IS NOT
NOT EQUAL
NEQ

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 contains the value on the right.

DOES NOT CONTAIN

Opposite of CONTAINS. Return True if the value on the left does not contain the value on the right.

GREATER THAN
GT

Return True if the value on the left is greater than the value on the right.

LESS THAN
LT

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
GTE
GE

Return True if the value on the left is greater than or equal to the value on the right.

LESS THAN OR EQUAL TO
LTE
LE

Return True if the value on the left is less than or equal to the value on the right.

Note:

In CFScript expressions only, you can also use the following decision operators. You cannot use them in expressions in tags. == (EQ), != (NEQ), > (GT), < (LT), >= (GTE), and <= (LTE).

Decision operator rules

The following rules apply to decision operators:

  • When ColdFusion evaluates an expression that contains a decision operator other than CONTAINS or DOES NOT CONTAIN, it first determines if the data can be converted to numeric values. If they can be converted, it performs a numeric comparison on the data. If they cannot be converted, it performs a string comparison. This can sometimes result in unexpected results. For more information on this behavior, see Evaluation and type conversion issues in Data type conversion.
  • When ColdFusion evaluates an expression with CONTAINS or DOES NOT CONTAIN it does a string comparison. The expression A CONTAINS B evaluates to True if B is a substring of A. Therefore an expression such as the following evaluates as True:

123.45 CONTAINS 3.4

  • When a ColdFusion decision operator compares strings, it ignores the case. As a result, the following expression is True:

    "a" IS "A"

  • When a ColdFusion decision operator compares strings, it evaluates the strings from left to right, comparing the characters in each position according to their sorting order. The first position where the characters differ determines the relative values of the strings. As a result, the following expressions are True:

    "abde" LT "ac"

String operators

String operators manipulate strings of characters. The following table describes the operators:

Operator

Description

&

Concatenates strings.

&=

Compound concatenation. The variable on the right is used as both an element in the concatenation operation and the result variable. Thus, the expression a &= b is equivalent to a = a & b.An expression can have only one compound assignment operator.

Note:

In a Query of Queries, you use || as the concatenation operator.

Ternary Operator

The ternary operator is a decision operator with three operands. It assigns a variable a value based on a Boolean expression. The operator has the form

(Boolean expression)? expression1 : expresson2

If the Boolean expression evaluates to true, the operator result is expression1; otherwise, it is expression2
For example

<cfset c = (a GT b)? a : b >

If a is greater than b, c is assigned the value of a; otherwise, c is assigned the value of b. 
The parentheses can contain any expression that evaluates to a Boolean value, and a and b can be any valid expression. You can nest this operator inside other expressions.

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. (Some alternative names for operators, such as EQUALS and GREATER THAN OR EQUAL TO are omitted for brevity.)

^
*, /
\
MOD
+, -
&
EQ, NEQ, LT, LTE, GT, GTE, CONTAINS, DOES NOT CONTAIN, ==, !=, >, >=, <, <=
NOT, !
AND, &&
OR, ||
XOR
EQV
IMP

To enforce a nonstandard order of evaluation, parenthesize expressions. For example:

  • 6 - 3 * 2 is equal to 0
  • (6 - 3) * 2 is equal to 6
    You can nest parenthesized expressions. When in doubt about the order in which operators in an expression are 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:

  • Rand()
  • UCase("This is a text: ") & ToString(123 + 456)

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 number 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:

  • Replace("Eat and Eat", "Eat", "Drink") returns "Drink and Eat"
  • Replace("Eat and Eat", "Eat", "Drink", "All") returns "Drink and Drink"
    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 myStringVar = UCase(firstVariable & " more sleep!")>

When ColdFusion server executes the second line, it does the following:

  1. Identifies 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 running the function.

Using multiple assignments in one expression

You can chain assignments to assign the same value to multiple variables in a single statement. This includes chain assignments for the results of an expression. The following code displays a chain assignment:

a=b=c=d*5

You can use the var operator in multiple assignments, but the variables with this operator must precede all others. For example:

//The following line is valid.
var a = var b = c = d*5
//The following line is not valid.
// a = b = var c = d*5

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy