Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 59

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
The CFML Programming Language
Comparing variables to True or False
You might expect the following two
<cfif myVariable>
<cfoutput>myVariable equals #myVariable# and is True
</cfoutput>
</cfif>
<cfif myVariable IS True>
<cfoutput>myVariable equals #myVariable# and is True
</cfoutput>
</cfif>
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, use the test
<cfif
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
or
, it checks whether it can convert each string value to a number or date-time object. If so, ColdFusion converts
IS
GT
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 can 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
, instead of the comparison operators.
CompareNoCase
tag examples to produce the same results:
cfif
testvariable
, and not use the
>
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.
Last updated 1/20/2012
comparison operator when testing whether a variable
IS
54
and
Compare

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents