Data Types
ColdFusion variables can hold many different data types. A data type indicates what kind of information is being stored. ColdFusion variables can store data types, such as numbers, text strings, dates, times, and Boolean values, just to name a few. See Table 2.1 for further information on various ColdFusion data types.
Table 2.1 ColdFusion Data Types
Data Type |
Description |
Integers |
These are whole numbers (numbers without anything to the right of the decimal point), such as 0, 17, and 105. |
Real numbers |
Real numbers, also referred to as floating-point numbers, are numbers that might include a decimal value, such as 3.17, 0.175, and 25.387. |
Strings |
Strings are a sequence of symbols, such as letters, numbers, and special characters. |
|
Strings are enclosed by either single or double quotes. For example, "ColdFusion Mentor", 'user@anysite.com', and "10". |
Booleans |
A Boolean value is either True or False. For example, <CFSET UserIsLoggedIn = True>. |
|
Boolean values can be expressed in a number of ways. Negative values can be False, No, or 0. Positive vales can be True, Yes, or 1 (or any nonzero number). |
Date-time |
Date-time values can be date only, time only, or a combination of both the date and the time, such as the result of the Now() function. |
Lists |
A list is a string that consists of multiple entries separated by some type of delimiter. The comma is the default delimiter, but others can be specified. Lists will be explained in Step 10. An example might be <CFSET Colors="Red,White,Blue">. |
Arrays |
This complex data type stores information in a table-like structure of rows and columns. Arrays will be explained in Step 10. |
Structures |
This complex data type stores information in a series of key-value pairs. Structures will be explained in Step 10. |
Queries |
This complex data type holds the results of a database query. |
Binary |
Binary data is raw data, such as the contents of a file or an executable program. |
Object |
These are complex object types that are created using the <CFOBJECT> tag. They can include things like COM, Java, and CORBA objects. |
You create most ColdFusion variables simply by giving them a name and assigning them a value. We have already seen one way to accomplish this by using the <CFSET> tag. For example, the line of code
<CFSET Age="Thirty">
would automatically create a variable called Age and assign it a string value of "Thirty".
Many other programming languages require you to set the type of value that a variable will hold before you assign it a value. For example, you would have to declare the Age variable to be a string variable before you could assign a string value, such as "Thirty" to it. However, ColdFusion variables are typeless, meaning you are not required to assign a specific data type to a variable name. ColdFusion automatically evaluates variable values when they are used in operations to determine how the variable should be used. For a further explanation of this, see the following sidebar.
Is It a Number or Not?
Because ColdFusion variables are typeless, there can be some confusion about the value of some strings. For example, the code
<CFSET Age="30">
would set the value of the Age variable to the string value of "30" (note the quotation marks), not the integer value of 30. In stricter programming languages, a simple mathematical operation using this valuesuch as ("30"21would cause an error because you cannot subtract a number from a string. However, ColdFusion is a bit more clever than that. ColdFusion uses what is called operation-driven evaluation; this means that when ColdFusion sees a mathematical operation, such as subtraction or multiplication, it automatically tries to convert all the operands (elements in the equation) into numbers. So, in ColdFusion, even though the Age variable contains a string, instead of throwing an error, the code<CFSET Age="30"> <CFSET YearsOverTheHill= Age - 21> <CFOUTPUT>#YearsOverTheHill#</CFOUTPUT>would output the numerical value of 9.
Pretty clever, huh?
Conversely, if you use the following code to set the DaysTillXmas variable to the integer value of 30 (note that there are no quotation marks), it will be treated as a number.
<CFSET DaysTillXmas=30>
If we then try to combine it with text (as seen in the following code), ColdFusion will be smart enough to convert it to the string value of "30" rather than the number. For example, the code<CFSET DaysTillXmas=30> <CFSET Message="Only #DaysTillXmas# to go!"> <CFOUTPUT>Hey kids, Santa's coming. #Message# </CFOUTPUT>
would output
Hey kids, Santa's coming. Only 30 days to go!
Although ColdFusion is pretty clever, there might be times when you want to make sure that a variable gets evaluated as a number and not a string or vice versa. A couple of ColdFusion functions can help you do this. The Val() function will evaluate a string into a number (if possible). For example, the following line of code would convert the Age variable into a number:
<CFOUTPUT>This is the number #Val(Age)#</CFOUTPUT>
To convert a number into a string, you can use ToString(). For example, the following line of code converts an integer value of 30 into the string "30".<CFOUTPUT>This is the string #ToString(30)#</CFOUTPUT>
For more information on various ColdFusion functions, check out the "Language Reference" section of this book's web site at http://www.LearnColdFusionMX.com.