Operators  
 
 

Operators combine sub-expressions to create more complex expressions. The general syntax for using operators is:

Expression Operator Expression

For example, 2 * (3 + 4) is a valid expression in which the plus (+) operator is used to combine two numbers into an expression (which evaluates to 7), while the multiplication (*) operator is used to combine the number 2 with the expression (3 + 4) to produce the final result of 14.

ColdFusion has four types of operators:

  • Arithmetic operators
  • String operators
  • Decision, or comparison, operators
  • Boolean operators
 
 
  Arithmetic operators  
 
 

ColdFusion has nine arithmetic operators for addition, subtraction, multiplication, division, remainder calculation, integer division, exponentiation, and sign changing.

Arithmetic Operators 
Operator
Description
+, -, *, /

The basic arithmetic operators: addition, subtraction, multiplication, and division. In the case of division, the right operand cannot be zero.

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.

 
 
  Unary arithmetic operators  
 

There are two unary arithmetic operators for setting the sign of a number either positive or negative (+ or -). They modify the value as you would expect. For example:

  • +2 is 2
  • -2 is (-1)*2
 
 
  Examples  
 
<CFSET DoubleNumber=2 * Form.Number>
<CFSET Number=(2 + 3) * 2>
<CFIF Form.Number IS DoubleNumber / 2>
...CFML tags...
</CFIF>
 
 
  String operators  
 
 

In ColdFusion multiple strings can be concatenated with the & (ampersand) operator.

ColdFusion also supports the automatic concatenation of variable values and function return results delimited by pounds inside strings. For more information, see Using Pound Signs.

 
 
  Examples  
 
<CFSET Text1="Jack is not " & (Form.Height * 2)>
<CFSET Text2="This text " & "continues...">

You can also output strings using variables, as in this example:

<CFSET Text1="John ">
<CFOUTPUT>#Text1#Smith
</CFOUTPUT>
 
 
  Decision, or comparison, operators  
 
 

ColdFusion has eight decision, or comparison, operators that produce a Boolean TRUE/FALSE result based on the result of the test they perform on their two arguments.

Decision Operators
Operator
Description
IS
Performs a case-insensitive comparison of the two values and returns true if the values are identical.
IS NOT
Opposite behavior of is.
CONTAINS
Checks to see if the value on the left is contained in the value on the right and returns true if it is.
DOES NOT CONTAIN
Opposite behavior of contains.
GREATER THAN
Checks to see if the value on the left is greater than the value on the right and returns true if it is.
LESS THAN
Opposite behavior of greater than.
GREATER THAN OR EQUAL TO
Checks to see if the value on the left is greater than or equal to the value on the right and returns true if it is.
LESS THAN OR EQUAL TO
Checks to see if the value on the left is less than or equal to the value on the right and returns true if it is.

 
 
  Shorthand notation for Boolean operators  
 

You can replace some Boolean operators with shorthand notations to make your CFML more compact, as shown in the following table:

Shorthand Notation for Boolean Operators 
Operator
Alternative name(s)
IS
EQUAL, EQ
IS NOT
NOT EQUAL, NEQ
CONTAINS
Not available
DOES NOT CONTAIN
Not available
GREATER THAN
GT
LESS THAN
LT
GREATER THAN OR EQUAL TO
GTE, GE
LESS THAN OR EQUAL TO
LTE, LE

 
 
  Example  
 
<CFSET ResultValue=4 IS NOT Form.Number>

If Form.Number is not 4, ResultValue would be TRUE, which would be output as the string "TRUE".

 
 
  Boolean operators  
 
 

Boolean, or Logical, operators perform logical connective and negation operations. The operands of Boolean operators are Boolean (TRUE/FALSE) values. ColdFusion has the following six Boolean operators:

Boolean Operators 
Operator
Description
NOT
Reverses the value of an argument. For example, NOT TRUE is FALSE and vice versa.
AND
Returns TRUE if both arguments are TRUE; returns FALSE otherwise. For example, TRUE AND TRUE is TRUE, but TRUE AND FALSE is FALSE.
OR
Returns TRUE if any of the arguments is TRUE; returns FALSE otherwise. For example, TRUE OR FALSE is TRUE, but FALSE OR FALSE is FALSE.
XOR
Exclusive or--either, or, but not both. Returns TRUE if the truth values of both arguments are different; returns FALSE otherwise. For example, TRUE XOR TRUE is FALSE, but TRUE XOR FALSE is TRUE.
EQV
Equivalence both true or both 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. A IMP B is the truth value of the logical statement "If A Then B." A IMP B is FALSE only when A is TRUE and B is FALSE.

The Boolean operators are most often used in CFIF and CFELSEIF tags to control the execution of CFML tags in an application page. They are also used in the CONDITION attribute of the CFLOOP tag. Boolean operators are also used in the IIf function.

 
 
  Examples  
 
<CFIF IsDefined("Form.FName") AND IsDefined("Form.LName")>
... CFML tags...
</CFIF>

<CFLOOP CONDITION="NOT (IndexValue LTE 1 OR IndexValue GTE 10)">
... CFML tags...
</CFLOOP>
 
 
  Operator precedence  
 
 

The order of precedence controls which operator is evaluated first in an expression. Operators on the same line have the same precedence.

 
 
  Operator precedence, highest to lowest  
 
Unary +, Unary -
^
*, /
\
MOD
+, -
&
EQ, NEQ, LT, LTE, GT, GTE, CONTAINS, DOES NOT CONTAIN
NOT
AND
OR
XOR
EQV
IMP

To enforce a specific non-standard order of evaluation, you must parenthesize expressions. For example:

  • 6 - 3 * 2 is equal to 0
  • (6 - 3) * 2 is equal to 6

Parenthesized expressions can be arbitrarily nested. When in doubt about the order in which operators in an expression will be evaluated, always use parentheses.



 
 
BackUp LevelNext
 
 

allaire     AllaireDoc@allaire.com
    Copyright © 1998, Allaire Corporation. All rights reserved.