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
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
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:
Expression
<cfif "1a" EQ "01:00">
<cfif "1P" GT "2A">
<cfset age="4a">
<cfset age=age + 7>
<cfif "0.0" is "0">
To prevent such ambiguities when you compare strings, use the ColdFusion string comparison
functions
Compare
You can also use the
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
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.
70
Chapter 3: Using ColdFusion Variables
<cfif testvariable>
or
, it checks whether it can convert each string value to a number or
IS
GT
Interpretation
If 1:00am is 1:00am.
If 1:00pm is later than 2:00am.
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.
If 0 is 0.
and
, instead of the comparison operators.
CompareNoCase
function to determine whether a string can be interpreted as a date-
IsDate
Now
, and not use the
,
,
CreateDate
CreateTime
comparison
IS
, and
CreateDateTime
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers