Accessibility
 
 
ColdFusion Expressions: Operands, Operators, and Other Constructs

By: Mary Horvath, Allaire Documentation Group

This technical note describes ColdFusion expressions -- language constructs that allow you to create sophisticated applications.

Click here to download the pdf version of this document.

Contents

Section A

Section B

Section A—Read below.
Section BClick here to read.

Elements of ColdFusion Expressions

ColdFusion expressions consist of operands and operators. Constants and variables comprise the operands. Operators are the verbs that act on the operands; functions are a form of operator. This chapter first describes the operands and then describes the operators.

Operands: Data Types, Constants, and Variables

The sections on operands provide detailed information on the following:

  • Data types of ColdFusion operands
  • ColdFusion constants
  • ColdFusion variables: naming conventions, kinds of variables, scoping, and other relevant details

Data Types of Constants and Variables

In ColdFusion, constants and variables have implied data types. The reason for the term "implied" is that there are no variable declarations that define the scalar data type of a variable as one finds in statically typed languages such as Pascal. Whenever you use a constant or set the value of a variable, the value you use has a type, be it integer, real, or string. For example, if you use CFSET to create a variable AGE and set its value to 62, the implied data type is integer. In addition to these scalar types, ColdFusion also supports lists, structures, and arrays. The following table describes the types of variables and constants you can use in a ColdFusion application page.

Data Types
Type Description

Integers

Numbers with no decimal point. Integers have both a lower and an upper limit. The lower limit for a signed integer is

-2,147,483,648, and the upper limit for a signed integer is 2,147,483,647.

If you specify an integer that exceeds the upper or lower limit, ColdFusion converts the number to a floating point value to avoid overflow. In doing so, ColdFusion preserves the number's magnitude. However, the conversion to floating point comes at the expense of precision, since ColdFusion has only a fixed number of bits to work with.

Real numbers

Numbers with a decimal part. Also known as floating point numbers.

The range of ColdFusion numbers is approximately ±10300, or ±1 with 300 zeros after it. Most operations are accurate to 12 digits after the decimal point.

ColdFusion supports scientific notation; this means that you can enter numbers in the form 3 E 16 and perform calculations.

Strings

Text values, which can be enclosed in single (') or double (") quotes. Strings length is limited only by the amount of available memory on the ColdFusion server.

To use a single quote inside a string that is single quoted, escape the single quote by using two single quotes. You can similarly escape a double quote inside a double quote-enclosed string. For example:

"the ""best"" software"

To insert a pound sign in a string, the pound sign must be escaped, or doubled. For example:

"use a pound sign (##) here"

Boolean values

The result of a logical operation. Their value can be either TRUE or FALSE. The numerical value of TRUE is 1. The numerical value of FALSE is 0. When converted to a string, TRUE becomes "YES" and FALSE becomes "NO".

Date values

Date-and-time values identify a date and time in the range 100AD to 9999AD. If no time part is specified, time is set to 12:00am.

You can also directly enter a date object in any of the following formats:

"October 30, 1999"
"Oct 30, 1999"
"Oct. 30, 1999"
"10/30/99"
"1999-30-10"

Time values

You can directly enter a date-and-time object in any of the following formats:

"October 30, 1999 02:34:12"
"October 30, 1999 2:34a"
"October 30, 1999 2:34am"
"October 30, 1999 02:34am"
"October 30, 1999 2am"

The time part of the object is accurate to the second.

Lists

Lists are a special kind of string, made up of elements separated by delimiters.

The default delimiting character used by all list processing functions is a comma: (,). A list can have more than one delimiting character. You specify the allowable delimiters for a list in the delimiters parameter of ColdFusion list processing functions. White space is not considered a delimiter. However, when using lists where elements may be separated by white space as well as other delimiters, be sure to add the white space characters to the delimiters. Delimiters before the first element and after the last element are ignored.

The structure of lists is flat - that is, lists cannot be nested into one another. Also, lists can contain no "empty" elements. A list can be empty, however. The empty list is equivalent to the empty string "".

Structures

You can use structures to create and maintain key-value pairs, to refer to related string values as a unit rather than individually, or to create associative arrays.

For more information about structures, see Developing Web Applications with ColdFusion.

Arrays

Arrays are tables of objects or data that can be indexed. Although the ArrayNew function only supports creating up to three-dimensional arrays, there is no limit on array size or maximum dimension.

Elements stored in an array are referenced as follows:

<CFSET myarray[1][2]=Now()>

For more information about arrays, see Developing Web Applications with ColdFusion.

Queries

ColdFusion queries can be referenced as objects by assigning a query to a variable:

<CFQUERY NAME=myquery
    DATASOURCE=mydata
    SELECT * FROM CUSTOMERS
</CFQUERY>

<CFSET myquery2=myquery>

In this case (unlike the same operation with arrays) the query is not copied. Instead, both names point to the record set data so that if you make changes to the table referenced in the query, the original query and the query object myquery2 will both reflect those changes.

Queries and variables cannot have the same name at the same time in the same application page.

COM objects

COM (Component Object Model) objects are non-visual components that encapsulate specific functionality you can invoke in your application pages. ActiveX, OCX, CORBA, and ADO objects are examples of COM objects.

COM objects generally contain methods, like functions, you can use to execute certain operations:

<CFSET temp=Mailer.SendMail()>

COM objects also generally contain properties you can read and write using ColdFusion variables:

<CFSET Mailer.FromName=Form.fromname>

Properties can be invoked on either side of an assignment.

For more information about COM objects, see Developing Web Applications with ColdFusion.

Notes on date-and-time values

There are a variety of ways in which a date-and-time value can be entered in ColdFusion. You can use the functions that create date-and-time objects using various criteria.

How date and time values are stored

Internally to ColdFusion, date-and-time values are represented on a time line as a subset of the real numbers. This is done for efficiency in evaluation and because it directly mimics the method used by many popular database systems, including Microsoft Access. One day is equal to the difference between two successive integers. The time portion of the date-and-time value is stored in the fractional part of the real number.

Thus, arithmetic operations can be used to manipulate date-and-time values. For example, Now() + 1 will evaluate to tomorrow at the same time. However, we strongly discourage ColdFusion developers from using this potentially troublesome method of manipulating date-and-time objects. Date-and-time manipulation routines should be used instead.

21st century dates

Two-digit years from 00 to 29 are treated as 21st century dates; 30 to 99 are treated as 20th century dates.

"October 30, 2015"
"October 30, 15"

Fast Date/Time parsing in UNIX

On UNIX, there is a switch that provides fast date/time parsing. If you have enabled this switch, you must refer to dates in expressions in the following order: month, day, and year. For example:

<CFIF "11/23/1998" GT "11/15/1998">

If this switch is set, the default date format returned by the DateFormat() function cannot be parsed in an expression. However, to parse this date specify a mask indicating the correct order, such as, mm/dd/yyyy.

The Fast Date/Time Parsing switch is set on the ColdFusion Administrator Server Settings page. Refer to Administering ColdFusion Server for more information about ColdFusion settings.

Constants

Constants are simple scalar values that you can use within expressions and functions. They include integers, real numbers, time and date values, boolean values, string values, and lists. Constants do not include more complex values, such as structures and arrays, because arrays and structures are created as variables.

Variables

Variables are the most frequently used operands. Unlike constants, which always have the same value, their values can be set and reset, passed as attributes to CFML tags and JRun servlets. Like constants, they can also be passed as parameters to functions, and can replace most constants. The following sections discuss:

For a comprehensive list of all of the variables associated with ColdFusion, see Quick Reference to CFML.

Variable naming conventions

When naming ColdFusion variables and form fields, keep these guidelines in mind:

  • Variable names must begin with a letter, which can be followed by any number of letters, numbers, or the underscore character.
  • Variable names must be all one word.
  • Do not use spaces or special characters in variable names.
    For example, UserName_1, UserName2, and User_Name are valid, but 1stUser, WhatAName!, and User-Name are not.
  • For field names and variables, use descriptive names, not abbreviations. It will be much easier for others to read your code, and for you to remember how it works yourself, when you revisit it months later.
  • Queries and variables cannot have the same name in the same ColdFusion application page.
  • Although ColdFusion variables are not case-sensitive, keep capitalization consistent in order to keep your code consistent.
  • In writing queries and forms, match your form field names with the corresponding database field name.
  • In some cases, a variable must have pounds signs around it to allow ColdFusion to distinguish it from string or HTML text and to insert its value as opposed to its name. For more information on how to use pound signs in expressions see Pound Signs.

Kinds of variables

ColdFusion supports several kinds of variables. This section describes the kinds of variables available and how you use them.

The following table describes the types of variables you can use in a ColdFusion application page.

Kinds of ColdFusion Variable  
Kind of Variable Description

Queries

As soon as a query has been run, you can use its results as dynamic parameters. For example, if you create a query named LookupUser that finds the ID for a given user name, you can use this ID in another query or in a call to CFOUTPUT.

Local Variables

This is the default scope for variables created with the CFSET and CFPARAM tags. For example, <CFSET A=5> sets the variable A to 5. This variable is available only on the page where it is created and any included pages.

URL Parameters

Parameters appended to URLs after the application page name using a variablename=value format. URL parameters are stored in structures. See Variables stored in structures for more details.

Form Fields

The most common way of passing parameters to a page is to use form fields. When a user enters data in a form field, a parameter with the name of the form field is passed to the action page. Form fields are stored in structures. See Variables stored in structures for more information.

Client

ColdFusion client variables are variables associated with a particular client. Client variables allow you to maintain state as a user moves from page to page in an application. They are stored in the system registry by default, but you can also choose to store them in a cookie or in a database.

Client variables are part of the ColdFusion Web Application Framework. For more information about client variables in this context, see Developing Web Applications with ColdFusion.

Server

ColdFusion server variables are associated with the current Web server and are available to all ColdFusion applications until the ColdFusion server is shut down. This server scope allows you to define variables that all your ColdFusion application pages can reference.

Server variables are stored in structures. See Variables stored in structures for more information.

For information on locking server variables, see the description of CFLOCK in CFML Language Reference.

Session

Session variables are tied to an individual client and persist for as long as that Client ID maintains a session. Session variables, like current client variables, require a client name to work and are only available to that Client ID.

Unlike client variables, session variables are stored in the server's memory and can be set to time-out after a precise period of inactivity.

Session variables are stored in structures. See Variables stored in structures for more details.

Session variables should be locked when accessed. For information on locking session variables, see the description of CFLOCK.

Session variables are part of the ColdFusion Web Application Framework. This means that they are used in the context of an Application.cfm page. For more information about session variables in this context, see Developing Web Applications with ColdFusion and the description of the CFAPPLICATION tag.

Application

Application variables are tied to an individual application as defined in the CFAPPLICATION NAME attribute, typically used in the Application.cfm file. Application variables only work if an application name has been defined.

Application variables are stored in structures. See Variables stored in structures for more information.

Application variables should be locked when accessed. For information on locking session variables, see the description of CFLOCK in CFML Language Reference.

Application variables are part of the ColdFusion Web Application Framework. This means that they are used in the context of an Application.cfm page. For more information about session variables in this context, Developing Web Applications with ColdFusion and the description of the CFAPPLICATION tag.

Request

Request variables store data that pertains to the processing of a single page request.

They are a convenient way to store data in a structure, carried through nested tags, such as Custom Tags, and processed at once.

Request variables are stored in structures. See Variables stored in structures for more information.

Caller

Caller variables let you call and use variables from the calling template in a ColdFusion custom tag.

HTTP Cookies

HTTP Cookie variables are stored in the browser. They are available every time the browser makes a page request. You can create cookie variables with the CFCOOKIE tag.

File

File variables are read-only. These variables are created when a CFFILE ACTION=UPLOAD operation is executed.

CGI Environment

Every page request has several environment variables sent to it that relate to the context in which it was requested. The variables available will depend on the browser and server software in use for a given request.

Note: CGI environment variables are created even when you are using one of the Web servers that supports a server API.

The section Order of ColdFusion variable look-ups describes the order in which ColdFusion finds variables in application pages.

Qualifying, or scoping, variable references

ColdFusion distinguishes between identically named parameters from different sources with a specific prefix for each source or "scope." For example, to specify a variable called State that is passed in a form submission, you would use Form.State. To specify a variable named State passed in a URL, you would use URL.State.

Although you aren't required to use the prefix unless two variables in different scopes have the same name, for readability and processing speed, it is recommended that you use prefixes. For example, the variable "Form.lastname" is far more self-evident than a variable called "lastname."

The following chart shows the prefixes for each variable type:

Variable Prefix 
Kind Reference

Queries

QueryName.variablename

Local

Variables.variablename

URL Parameters

URL.variablename

Form Fields

Form.variablename

Client

Client.variablename

Server

Server.variablename

Session

Session.variablename

Application

Application.variablename

Request

Request.variablename

HTTP Cookies

Cookie.variablename

File

CFFILE.variablename

CGI Environment

CGI.variablename

Tip Always use the variable's scope prefix for Application, Session, Server, and Request variables.

Many of the CFML tags have variables associated with them, such as CFERROR and CFFTP. Typically, these variables use the tag name as the prefix. For a complete listing of these variables, and all other variables mentioned in this Technical Note, see Quick Reference to CFML.

Performance and scoping

You can improve performance by always qualifying your variables with the proper scope. Adding variable scopes improves processing speed but the trade-off is that it may not be so easy to reuse the same code in other applications or pages.

In the following example, both forms of the following variable are permitted. However, the example that includes a scoping prefix will be evaluated more quickly than the unscoped example:

<CFOUTPUT>
        #Client.fullname#
        #fullname#
</CFOUTPUT>

Order of ColdFusion variable look-ups

When scoping is not used, that is, when you don't include a variable prefix, such as "Form.myformvar," ColdFusion attempts to find variables in the following order:

  1. Local variables created using CFSET and CFQUERY
  2. CGI variables
  3. File variables
  4. URL variables
  5. Form variables
  6. Cookie variables
  7. Client variables
Note ColdFusion does not attempt to automatically find Application, Session, Server, and Request variables, nor does it try to find CFML tag variables, with the exception of File variables. You must use prefixes with these variables.

Variables across application pages

Within ColdFusion, most variables apply only to a single template. However, several types of variables can be used across multiple application pages:

  • Client variables store identifying information about an individual client.

    Client variables are designed to hold long-term information particular to an individual client. You can store client variables in the system registry, a database, or in a cookie named for the application.

  • Server variables are valid for all ColdFusion applications on a particular server.

    Server variables are used to store information (typically read-only) that does not change often and can be shared across many users and ColdFusion applications.

  • Session variables are valid for as long as an individual Client ID maintains a session.

    Session variables are useful for storing short-term information needed for a single site visit or set of requests. They are specific to individual users.

  • Application variables are valid for a specified application.

    Application variables are available to individual, specified applications and to all users of that application who access your ColdFusion server. They are stored in the server's memory and can be set to time out at specified intervals. Application variables are general and available to all users.

  • Caller variables are valid for ColdFusion custom tags.

    Caller variables are available for passing information among application pages for a ColdFusion custom tag.

Note In cases where variables are available to several applicati, make sure to keep track of your variable names. See Variable naming conventions for more information.

See the description of CFAPPLICATION for information on setting up client state management using the CLIENTMANAGEMENT attribute in the CFAPPLICATION tag. See also Developing Web Applications with ColdFusion for information about using Application, Client, and Session variables.

Variables stored in structures

As of ColdFusion 4.5, Form, Application, Session, Server, Request, and URL variables are stored in structures. Thus, you can reference all of the fields taken from a form as one structure, all parameters passed on a URL as a structure, and so forth.

Structures can be accessed using any of the ColdFusion structure functions, and in the form structname.key, where each key represents a field name of a form or a variable name of a specific type. Because these structures are formed by ColdFusion whenever these variables are used, it makes conversion into WDDX particularly straightforward, if you need to package variable/form data into WDDX. For example:

<CFIF isDefined("form.submit")>

    <CFWDDX action="CFML2WDDX" input="#form#" output="formscope">
    <CFOUTPUT>#formScope#</CFOUTPUT>

</CFIF>

<CFOUTPUT>
<form method="post" action="myForm.cfm">
    <input type="Text" name="Title"><br>
    <input type="Text" name="URL"><br>
    <textarea name="teaser" rows="3" cols="40"></textarea>
    <br>
    <input type="Submit" name="submit" value="Submit">
</form>
</CFOUTPUT>
Note Do not use StructClear to clear session and application variables because this can cause the loss of SessionID, CFID, and CFTOKEN variables, as well as application variables. In order to prevent this behavior, store session data in session.foo.* and then clear session.foo, and store application data in application.foo.* and then clear application.foo.

Click here to continue reading.