# Writing Expression ColdFusion Help

Now that you have- learned to use variables and functions: you will turn to a power ful tool for leveraging them: expressions.

Expressions provide a way to perform operations on one or more pieces of data. These can range from simple operations, such as addition and subtraction, to more complex operations, such as comparison and type conversion.

In this chapter, you will start with a quick overview of expressions and then consider the types of operators available for building expressions in ColdFusion. Next you will look at some of the issues surrounding the use of pound signs and quotation marks in ColdFusion expressions.

After you have a grasp of the structure of these basic building blocks of expressions you will review the data types available in ColdFusion. These data types include string numeric and Boolean data and all have different roles.

Finally you will look at some interesting features of expressions in ColdFusion including order of precedence rules and type less expression evaluation.

Understanding Expressions

Expressions provide a way to build simple and complex operations to be performed on data. Expressions are built out of two essential components: data and operators

The data used in expressions can come from many sources, including the following:

• Literal values (such as 98 or n He110′)
• Variables referred to by name
• The results of a function call (such as Exp( 10)

Operators provide the glue that brings together multiple pieces of data, performing actions on them and providing the capabilities that make expressions useful. You will review the available ColdFusion operators in detail, but first let’s discuss expressions it general a bit more.

An important aspect of an expression is that it returns a single value. By taking the data and operators that make up the expression, ColdFusion distills the logic down to i final value. As an example, consider the simple mathematical expression 1 + 1. Here
you have two pieces of data (1 and 1) plus an ‘operator (+). This expression, as we all know, evaluates to 2.

This is an important aspect of expressions. Because they evaluate to a single value they can effectively be used wherever a literal: variable or other value is used in Cold-Fusion. For instance you could use this expression as a parameter to a function (Exp(l +1), or in a CFOUTPUTblock «CFOUTPUT>#va1(1 +l)#</CFOUTPUT». In all cases.

it is the final evaluated value of the expression that is used as an argument to a function or displayed to the user, or applied wherever the expression is used.

In its simplest form an expression is a single value such as a literal (for example 32) a variable name or a function call (for example Exp(32). At the next level of complexity expressions combine two values using a single operator:

Value Operator Value

Taking things even further an expression can be built out of other expressions combined with an operator

Expressions are differentiated by the type of data they return. Before getting into the details of operators and how they are used to build expressions, you need to consider the data types available in ColdFusion

Using ColdFusion Data Type.

ColdFusion has several data types including the following:

• Numbers
• Strings
• Boolean values
• Date/time values
• Lists
.- Arrays
• Structures·
• Queries
• Component Object Model (COM) objects

Numbers strings Boolean values and date/ time values make up the core set of simple data types available in Cold.Fusion, and we will discuss them in this chapter. Lists arrays and structures are complex data types built from these simple data types.)We discuss these three types in Chapter 14, “Working with ColdFusion Data Structure.

Similarly queries are a complex data type that you will encounter throughout.this book. However, the best place to learn about queries is in Chapter 10, “Using CFQUERY and~L to Interact with the Database.” Finally, COM objects are data types derived from working with external objects, a subject discussed in Chapter 32 “Including External Objects.”

Numbers

Numeric data in ColdFusion is much simpler than many other programming environments. Some development languages distinguish between integer and floating-point or real numbers. This distinction is not significant in ColdFusion. In ColdFusion numbers can be integers or floating-point numbers and are essentially used interchangeably wherever numbers are called for in your templates.

Integers are numbers ‘with no decimal portion. That is, they are whole numbers, such as 3, 76 I arid -5, with no fraction component after the decimal point. Real numbers, on the other hand, include a decimal component such as -1. 35 and 3.1415.

Valid numbers in Cold fusion range from -10300 to 10300.A valid number such as 10300 is the same as a 1 with 300 zeros following it. Although ColdFusion supports these surprisingly small and large numbers this does not mean it has unlimited accuracy.

When computers deal with floating-point numbers, they must deal in approximations. The level of accuracy of an environment refers to the number of digits after the decimal point that can be considered accurate. In ColdFusion, accuracy is up to 12 places after the decimal point. In practical terms, this means that if you add two numbers with 15 digits after the decimal point, only the first 12 digits after the decimal point in the result can be confidently considered accurate.

A nice feature of ColdFusion is that it supports scientific notation in indicating numbers. Scientific notation is commonly used in mathematics, engineering, and other sciences to indicate very large or very small numbers.

In traditional form scientific notation takes the form X x 10Y, where X is a real number between 1.0 and 10 (but not including 10) and Y is an integer. In the formula shown here, the lowercase x represents multiplication.

What this means is that 250,000 could be written as 2  5 x 100 I 000 or, in full scientific notation, 2.5 x 105.

In ColdFusion scientific notation is written somewhat differently than its traditional mathematical form but the meaning is exactly the same. The ColdFusion form is XEY Therefore, 250,000 becomes 2. SES.

Strings

String data consists of any arbitrary series of characters (letters numbers punctuation spaces or other visible ASCll characters) surrounded by single or double quotes. Examples of strings include:

• ‘Hello’
• ‘This is a test’
• ‘12345’

This last string is interesting because it looks like a number. But the quotation marks around it make it a string. Strings work somewhat differently than numbers: ‘There are different operators for working with strings as well as different functions and different roles.

An important issue to consider when working with strings is: How do you include a quotation mark inside a string?

Well it is not as complicated as it seems. If you enclose your string in double quotes then you can include single quotes in your string. Therefore the following string is valid: “What’s up?”. Similarly enclosing your string in single quotes allows double quotes to be used in the string making this string valid: ‘He said: ·What?·

But what if you want to include a double quote inside a string enclosed with double quotes or a single quote inside one enclosed with single quotes? To do this you must use a special technique called escaping. This tells ColdFusion not to use the character for.its special meaning (such as treating a double quote as the end of a string) but rather to include the value in the current data as if it were any other character.

In ColdFusion, characters are escaped by repeating them. Therefore, you can include single quotes in a string enclosed with single quotes by repeating the single quotes: ‘What’ ‘s up?’. You can do the same with double quotes: “He said: ··What?···.

Another character requiring escaping inside strings is the pound sign. Because of its special meaning in ColdFusion, pound signs in strings must also be escaped: •He is warning ##3.

Boolean Values

Boolean values are used to express data that has an either-or state. Any value that can either be true or false is a Boolean value. Boolean data can take two po¥ible values: True or False. These two Boolean values can also be expressed as numbers or strings, as shown in Table 6.1.

. TAB LE 6. 1:

Date time Values

Date/time values are used to represent any date and time cobination from A.D. 100 to .D. 9999 (wl)idl makes ColdFusion Year 2000-compliant) .

Internally ColdFusion represents date/time values as special objects which if displayed  to users appear in a less than ideal form such as:

{ts ‘1999-03-04 08:41:26’}

ColdFusion provides a range of functions (discussed in Appendix C “ColdFusion Function Reference”) for creating, manipulating, and displaying dates.

In addition when a date/time value is needed in an expression or as an argument to a function it can be expressed as a string in one of the following forms:

• “March 21, 1999″
•”Mar 21, 1999”
• “Mar. 21, 1999”
• “03/21/99”
• “1999-21-03”

Without times specified in a date/time value, the time is set to 12:00 A.M. To specify a time simply include the time in one of the following forms after the date:

• 01:23:45
• 1:23a
• 1:23am·
• 01:23am
• 1 am

For instance, the following are both valid date/time values:

• “Mar 21, 1999 1:23am”
• “March 21, 1999 01:23:45”

Special care needs to be taken when using two-digit year values as in 03/21/99. Although ColdFusion can handle dates beyond the year 2000 the following rules apply to two-digit year values:

• Years from 00 to 29 are treated as being in the 21st century (such as 2001 and 2010).
• Years from 30 to 99 are treated as being in the 20th century (such as 1945 and 1999).

Posted on November 14, 2015 in Writing Expression