ColdFusion automatically converts between data types to satisfy the requirements of an expression's operations, including a function's argument requirements. As a result, you generally don't need to be concerned about compatibility between data types and the conversions from one data type to another. However, understanding how ColdFusion evaluates data values and converts data between types can help you prevent errors and create code more effectively.
Operation-driven evaluation
Conventional programming languages enforce strict rules about mixing objects of different types in expressions. For example, in a language such as C++ or Basic, the expression ("8" * 10) produces an error because the multiplication operator requires two numeric operands and "8" is a string. When you program in such languages, you must convert between data types to ensure error-free program execution. For example, the previous expression might have to be written as (ToNumber("8") * 10).
In ColdFusion, however, the expression ("8" * 10) evaluates to the number 80 without generating an error. When ColdFusion processes the multiplication operator, it automatically attempts to convert its operands to numbers. Since "8" can be successfully converted to the number 8, the expression evaluates to 80.
ColdFusion processes expressions and functions in the following sequence:
- For each operator in an expression, it determines the required operands. (For example, the multiplication operator requires numeric operands and the CONTAINS operator requires string operands.)For functions, it determines the type required for each function argument. (For example, the Min function requires two numbers as arguments and the Len function requires a string.)
- It evaluates all operands or function arguments.
- It converts all operands or arguments whose types differ from the required type. If a conversion fails, it reports an error.
Conversion between types
Although the expression evaluation mechanism in ColdFusion is powerful, it cannot automatically convert all data. For example, the expression "eight" * 10 produces an error because ColdFusion cannot convert the string "eight" to the number 8. Therefore, you must understand the rules for conversion between data types.
The following table explains how conversions are performed. The first column shows values to convert. The remaining columns show the result of conversion to the listed data type.
Value |
As Boolean |
As number |
As date-time |
As string |
---|---|---|---|---|
"Yes" |
True |
1 |
Error |
"Yes" |
"No" |
False |
0 |
Error |
"No" |
True |
True |
1 |
Error |
"Yes" |
False |
False |
0 |
Error |
"No" |
Number |
True if Number is not 0; False otherwise. |
Number |
See "Date-time values" earlier in this chapter. |
String representation of the number (for example, "8"). |
String |
If "Yes", TrueIf "No", FalseIf it can be converted to 0, FalseIf it can be converted to any other number, True |
If it represents a number (for example, "1,000" or "12.36E-12"), it is converted to the corresponding number. If it represents a date-time (see next column), it is converted to the numeric value of the corresponding date-time object. |
If it is an ODBC date, time, or timestamp |
String |
Date |
Error |
The numeric value of the date-time object. |
Date |
An ODBC timestamp. |
ColdFusion cannot convert complex types, such as arrays, queries, and COM objects, to other types. However, it can convert simple data elements of complex types to other simple data types.
Type conversion considerations
The following sections detail specific rules and considerations for converting between types.
The cfoutput tag
The cfoutput tag always displays data as a string. As a result, when you display a variable using the cfoutput tag, ColdFusion applies the type conversion rules to any non-string data before displaying it. For example, the cfoutput tag displays a date-time value as an ODBC timestamp.
Case-insensitivity and Boolean conversion
Because ColdFusion expression evaluation is not case sensitive, Yes, YES, and yes are equivalent; False, FALSE, and false are equivalent; No, NO, and no are equivalent; and True, TRUE, and true are equivalent.
Converting binary data
ColdFusion cannot automatically convert binary data to other data types. To convert binary data, use the ToBase64 and ToString functions. For more information, see Binary data type and binary encoding.
Converting date and time data
To ensure that a date and time value is expressed as a real number, add zero to the variable. The following example shows this: