CFML provides a variety of operators that you can use to build expressions.
Operators fall into four category types:
Frequently used when coding conditional logic.
Frequently used when coding conditional logic.
The table below describes operators by type and symbol.
Type | Symbol | Description |
---|---|---|
Arithmetic | +, -, *, / |
Add, subtract, multiply, and divide.
In the case of division, the right operand cannot be zero.
For example, 9/4 is 2.25
|
\ |
Divides two integer values and returns a whole number.
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 .
| |
MOD |
Returns the remainder (modulus) after a number is divided.
The result has the same sign as the divisor.
The right operand cannot be zero.
For example, 11 MOD 4 is 3 .
| |
+ or - |
Set a number to either positive or negative.
For example, +2 is 2 and -2 is (-1)*2 .
| |
String | & | Concatenates text strings including those returned from variables and functions. |
Comparison | IS |
Performs a case-insensitive comparison of two values.
Returns true or false.
For example, this code tests for equal values. The condition is true only if the FirstName value is Jeremy.
<CFIF Form.FirstName IS "Jeremy"> |
IS NOT |
Opposite behavior of IS.
For example, this code tests for inequal values. The condition is true only if the FirstName value is not Jeremy.
<CFIF Form.FirstName IS NOT "Jeremy"> | |
CONTAINS |
Checks to see if the value on the left is contained in the value on the right.
Returns true or false.
For example this code tests for a value condition. The condition is true only if the FirstName value contains Jeremy.
<CFIF Form.FirstName CONTAINS "Jeremy"> | |
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. Returns true or false. | |
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. Returns true or false. | |
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. Returns true or false. | |
Compound Boolean | 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 argument is true; returns false otherwise. For example, TRUE OR FALSE is true, but FALSE OR FALSE is false. | |
XOR | Returns true if either argument is exclusively true; returns false otherwise. For example, TRUE XOR TRUE is false, but TRUE XOR FALSE is also true. | |
EQV | Returns true if both arguments are true or both are false. For example, TRUE EQV TRUE is true, but TRUE EQV FALSE is false. |
![]() |
Note: | You can replace some conditional operators with shorthand notations. Refer to the CFML Language Reference for ColdFusion Express for more information. |
Although form variables may pass to action pages, their values may not always be usable - for example a value of null.
The code below would be added to a form's action page to check for a value other than null ("") in a LastName form variable.
<CFIF Form.LastName IS NOT ""> Last Name: <CFOUTPUT>#Form.LastName#<BR></CFOUTPUT> <CFELSE> Last Name Not Entered!<BR> </CFIF>
During the next procedure, use this expression syntax on your action page to check for the value of a variable before trying to use it.
![]() |
To test for a variable's value: |
ActionPage.cfm
in HomeSiteYou will replace this code as you go.
<CFIF Form.LastName IS NOT "">
Last Name: <CFOUTPUT> #Form.LastName#<BR> </CFOUTPUT>
Last Name Not Entered!<BR>
<CFIF Form.LastName IS NOT ""> Last Name: <CFOUTPUT> #Form.LastName#<BR> </CFOUTPUT> <CFELSE> Last Name Not Entered!<BR> </CFIF>
ActionPage.cfm
should return all the values that you entered on the form.
If you receive errors, read the error message and check for spelling mistakes on the action page.
You should see the message that you coded.
Click here to see what results you should get.
Click here to see the new code on the action page.
Move on in this chapter to learn how to automatically redirect users from within your applications.