Using Operators to Build Expressions

CFML provides a variety of operators that you can use to build expressions.

Operators fall into four category types:

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 Note:You can replace some conditional operators with shorthand notations. Refer to the CFML Language Reference for ColdFusion Express for more information.

Usage example

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.

Note To test for a variable's value:
  1. Open ActionPage.cfm in HomeSite
  2. Delete Last Name: #Form.LastName#<BR> code from within the CFOUTPUT block.
  3. You will replace this code as you go.

  4. Add a CFIF tag directly above the existing CFOUTPUT block to test whether or not the user entered information in the LastName text field:
  5. <CFIF Form.LastName IS NOT "">
    
  6. Add a true procedure after the CFIF tag to output a label and the form variable.
  7. Last Name: 
    <CFOUTPUT>
        #Form.LastName#<BR>
    </CFOUTPUT>
    
  8. Add a CFELSE tag.
  9. Add a false procedure after this tag to output a message to the user.
  10. Last Name Not Entered!<BR>
    
  11. Add an ending CFIF tag to end the conditional logic statement.
  12. Your conditional logic statement should look like this:
  13. <CFIF Form.LastName IS NOT "">
        Last Name:
        <CFOUTPUT>
            #Form.LastName#<BR>
        </CFOUTPUT>
      <CFELSE>
        Last Name Not Entered!<BR>
    </CFIF>
    
  14. Save the page.
  15. View FormPage.cfm in a browser.
  16. Fill out each field and submit the form.
  17. 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.

  18. Return to the form in the browser.
  19. Reset the form.
  20. Do not enter a last name and submit the form again.
  21. 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.