User Guide

Table Of Contents
62 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.
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.