Working with Expressions


I've used the term expressions a few times in this chapter. What is an expression? The official ColdFusion documentation explains that expressions are "language constructs that allow you to create sophisticated applications." A better way to understand it is that expressions are strings of text made up of one or more of the following:

  • Literal text (strings), numbers, dates, times, and other values

  • Variables

  • Operators (+ for addition, & for concatenation, and so on)

  • Functions

So, UCase(FirstName) is an expression, as are "Hello, my name is Ben", 12+4, and DateFormat(Now()). And even though many people find it hard to articulate exactly what an expression is, realize that expressions are an important part of the ColdFusion language.

Building Expressions

Expressions are entered where necessary. Expressions can be passed to a <cfset> statement as part of an assignment, used when displaying text, and passed to almost every single CFML tag (except for the few that take no attributes).

Simple expressions can be used, such as those discussed previously (variables, functions, and combinations thereof). But more complex expressions can be used, too, and expressions can include arithmetic, string, and decision operators. You'll use these in the next few chapters.

When using expressions, number signs are used to delimit ColdFusion functions and variables within a block of text. So, how would you display the # itself? Look at the following code snippet:

 <cfoutput> #1: #FirstName# </cfoutput> 

You can try this yourself if you so feel inclined; you'll see that ColdFusion generates an error when it processes the code (see Figure 8.10).

Figure 8.10. Number signs in text must be escaped; otherwise, ColdFusion produces an error.


What causes this error? When ColdFusion encounters the # at the start of the line, it assumes you are delimiting a variable or a function and tries to find the matching # (which of course does not exist, as this is not a variable reference at all). The solution is to escape the number sign (flag it as being a real number sign), as follows:

 <cfoutput> ##1: #FirstName# </cfoutput> 

When ColdFusion encounters ##, it knows that # is not delimiting a variable or function. Instead, it correctly displays a single #.

When To Use #, and When Not To

Before we go any further, let's clarify exactly when number signs are needed and when they're not.

Simply put, number signs are needed to flag functions and variables within a string of text.

In this first example, the number signs are obviously needed:

 Hello #VARIABLES.FirstName# 

But what about when a variable is used within a tag, like this?

 <cfset UpperFirstName=UCase(FirstName)> 

Here number signs are not necessary because ColdFusion assumes that anything passed to a tag is a function or variable unless explicitly defined as a string. So the following is incorrect:

 <cfset #UpperFirstName#=#UCase(FirstName)#> 

This code will actually work (ColdFusion is very forgiving), but it is still incorrect and should not be used.

This next example declares a variable and assigns a value that is a string, so no number signs are needed here:

 <cfset FirstName="Ben"> 

But if the string contains variables, number signs would be necessary. Look at this next example: FullName is assigned a string, but the string contains two variables (FirstName and LastName) and those variables must be enclosed within number signs (otherwise ColdFusion will assign the text, not the variable values):

 <cfset FullName="#FirstName# #LastName#"> 

Incidentally, the previous line of code is functionally equivalent to the following:

 <CFSET FullName=FirstName & " " & LastName> 

Here number signs are not necessary because the variables are not being referred to within a string.

Again, the rule is: Only use number signs when referring to variables and functions within a block of text. It's as simple as that.



Macromedia Coldfusion MX 7 Web Application Construction Kit
Macromedia Coldfusion MX 7 Web Application Construction Kit
ISBN: 321223675
EAN: N/A
Year: 2006
Pages: 282

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net