70
Chapter 3: Using ColdFusion Variables
However, if myVariable has a numeric value such as 12, only the first example produces a result.
In the second case, the value of myVariable is not converted to a Boolean data type, because the IS
operator does not require a specific data type and just tests the two values for identity. Therefore,
ColdFusion compares the value 12 with the constant True. The two are not equal, so nothing is
printed. If myVariable is 1, "Yes", or True, however, both examples print the same result, because
ColdFusion considers these to be identical to Boolean True.
If you use the following code, the output statement does display, because the value of the variable,
12, is not equal to the Boolean value False:
<cfif myVariable IS NOT False>
<cfoutput>myVariable equals #myVariable# and IS NOT False
</cfoutput>
</cfif>
As a result, you should use the test
<cfif
testvariable
>
, and not use the
IS
comparison
operator when testing whether a variable is True or False. This issue is a case of the more general
problem of ambiguous type expression evaluation, described in the following section.
Ambiguous type expressions and strings
When ColdFusion evaluates an expression that does not require strings, including all comparison
operations, such as
IS
or
GT
, it checks whether it can convert each string value to a number or
date-time object. If so, ColdFusion converts it to the corresponding number or date-time value
(which is stored as a number). It then uses the number in the expression.
Short strings, such as 1a and 2P, can produce unexpected results. ColdFusion can interpret a
single "a" as AM and a single "P" as PM. This can cause ColdFusion to interpret strings as date-
time values in cases where this was not intended.
Similarly, if the strings can be interpreted as numbers, you might get unexpected results.
For example, ColdFusion interprets the following expressions as shown:
To prevent such ambiguities when you compare strings, use the ColdFusion string comparison
functions
Compare
and
CompareNoCase
, instead of the comparison operators.
You can also use the
IsDate
function to determine whether a string can be interpreted as a date-
time value, or to add characters to a string before comparison to avoid incorrect interpretation.
Date-time functions and queries when ODBC is not supported
Many CFML functions, including the
Now
,
CreateDate
,
CreateTime
, and
CreateDateTime
functions, return date-time objects. ColdFusion creates Open Database Connectivity (ODBC)
timestamp values when it converts date-time objects to strings. As a result, you might get
unexpected results when using dates with a database driver that does not support ODBC escape
sequences, or when you use SQL in a query of queries.
Expression
Interpretation
<cfif "1a" EQ "01:00">
If 1:00am is 1:00am.
<cfif "1P" GT "2A">
If 1:00pm is later than 2:00am.
<cfset age="4a">
<cfset age=age + 7>
Treat the variable age as 4:00 am, convert it to the date-time
value 0.16666666667, and add 7 to make it 7.16666666667.
<cfif "0.0" is "0">
If 0 is 0.
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......