Adobe COLDFUSION 9 Manual page 77

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
The CFML Programming Language
<cfset TheString = "Hello, FirstName!">
<cfset TheString = "Hello, " & "First" & "Name!">
As with the
statement, two expressions can be adjacent to each other in strings, as in the following example:
cfoutput
<cfset TheString = "Monk is #Left("Moon", 2)##Mid("Monkey", 3, 2)#">
The double-quotation marks around "Moon" and "Monkey" do not need to be escaped (as in ""Moon"" and
""Monkey""). This is because the text between the number signs is treated as an expression; it is evaluated before its
value is inserted inside the string.
Nested number signs
In a few cases, you can nest number signs in an expression. The following example uses nested number signs:
<cfset Sentence = "The length of the full name is #Len("#FirstName# #LastName#")#">
In this example, number signs are nested so that the values of the variables FirstName and LastName are inserted in
the string whose length the
Len
Nested number signs imply a complex expression that can typically be written more clearly and efficiently without the
nesting. For example, you can rewrite the preceding code example without the nested number signs, as follows:
<cfset Sentence2 = "The length of the full name is #Len(FirstName & " " & LastName)#">
The following achieves the same results and can further improve readability:
<cfset FullName = "#FirstName# #LastName#">
<cfset Sentence = "The length of the full name is #Len(FullName)#">
A common mistake is to place number signs around the arguments of functions, as in:
<cfset ResultText = "#Len(#TheText#)#">
<cfset ResultText = "#Min(#ThisVariable#, 5 + #ThatVariable#)#">
<cfset ResultText = "#Len(#Left("Some text", 4)#)#">
These statements result in errors. As a general rule, never place number signs around function arguments.
Using number signs in expressions
Use number signs in expressions only when necessary, because unneeded number signs reduce clarity and can increase
processing time. The following example shows the preferred method for referencing variables:
<cfset SomeVar = Var1 + Max(Var2, 10 * Var3) + Var4>
In contrast, the following example uses number signs unnecessarily and is less efficient than the previous statement:
<cfset #SomeVar# = #Var1# + #Max(Var2, 10 * Var3)# + #Var4#>
Dynamic expressions and dynamic variables
Many ColdFusion programmers never encounter or require dynamic expressions. However, dynamic variable naming
is important in situations where the variable names are not known in advance, such as in shopping cart applications.
ColdFusion also includes an
dynamically evaluates its arguments, and you often must use the
information on using the
IIF
function calculates.
function, which is most often used without dynamic expressions. This function
IIf
function, see
"Using the IIF
Last updated 8/5/2010
function to prevent the evaluation. For more
DE
function" on page 78
.
72

Advertisement

Table of Contents
loading

Table of Contents