Everything You Must Know About ColdFusion Variables
Understanding ColdFusion variables and logic is vital to being able to program in the language. In this tutorial, Jay's second covering ColdFusion, you will learn about the types of ColdFusion variables and how to use them.
Everything You Must Know About ColdFusion Variables - Data Type Category of Variables (Page 6 of 7 )
ColdFusion, unlike others, does not associate variable names with types. In this sense, it is "type less." However, the data of a variable usually has a type, and this affects how ColdFusion evaluates an expression, or a function. ColdFusion carries out automatic data conversions while evaluating expressions and functions. We will consider data type conversions later. The category of data types in ColdFusion can be any from the following:
Data Type
Brief Description
Examples
Simple Type
Integers
These are whole numbers. Decimal point not allowed.
0, -100, 75
Real Numbers
Numbers that might include a decimal value.
0.001, 100.001, -87.5
Strings
Strings are a sequence of symbols, such as letters, numbers, and special characters arranged like a word but not necessarily meaningful.
True can be expressed as Yes, 1, or any nonzero number.
False can take No or 0
Date-Time
Reference point in time.
Date
Time
Date and Time
Complex Type
Lists
A number of entries in a string separated by a delimiting character. The default delimiter is a comma.
1,2,3,4,5,6 is a list
Sun; Mon; Tue;Wed;Thur:Fri:Sat is also a list
Arrays
Stored data in a table structure accessible by an index or indices.
See example
Structures
Another complex data type, members accessed by a 'key' and its 'value'
See example
Queries
Results returned by a question posed to the database.
SELECT query results from a database
Binary Type
Binary
Raw data of an image GIF file, or a binary executable.
For example, Notepad.exe, dog.gif
Object Type
Object
Complex data and methods that work with data.
COM, CORBA, and Java type created by <cfobject> tag
Some data type category examples: Simple Type
These include numbers, strings, etc. Here is an example file, SimpleCat.cfm which has a couple of different data types.
<!--- This is an example of using Simple Type category--->
<cfoutput>Simple Type Category<br/></cfoutput>
<cfoutput><b>Next line, strg represents a string</b><br/></cfoutput>
<cfset strg="Welcome">
<cfoutput>
#strg#
</cfoutput><br/>
<cfoutput><b>Next line, n represents a positive integer</b><br/></cfoutput>
<cfset n=10 >
<cfoutput>
#n#
</cfoutput><br/>
<cfoutput><b>Next line, x represents a Boolean</b><br/></cfoutput>
<cfset x=True>
<cfoutput>
#x#
</cfoutput><br/>
<cfoutput><b>Next line, t represents a Date/Time</b><br/></cfoutput>
<cfset t=Now()>
<cfoutput>
#t#
</cfoutput><br/>
When this file is browsed with this address, http://localhost/TestingCF/SiampleCat.cfm we see the following displayed.
Complex Type
Arrays, Structures, XML documents, and so forth are the Complex Data Type Category in ColdFusion. Here are two examples, the first one being an array, (arrayed.cfm) and the next a structure, strut.cfm
Array Variable
<!---Defining an array variable with ArrayNew()--->
<!---Usage of ArrayLen()--->
<!---arrayed.cfm--->
<!--- ArrayNew() defines an array colors--->
<cfset colors=ArrayNew(1)>
<!---colors[1]="red" sets the first element of array as 'red'--->
<cfset colors[1]="Red">
<cfset colors[2]="Green">
<cfset colors[3]="Blue">
<!---ArrayLen(colors) evaluates the number of array elements
in 'colors' array--->
<cfset NoCol=ArrayLen(colors)>
<cfoutput>
#colors[1]# means danger<br/>
</cfoutput>
<cfoutput>
Number of colors in the array=#NoCol#
</cfoutput>
Browser display of the above file appears as follows;
Structure variable
<!--Example of a simple structure--->
<!---Usage of StructNew(), StructInsert()--->
<!---A complex employee variable is created by suign StrctNew()--->
<cfset employee=StructNew()>
<!---Method to insert a structures member--->
<cfset StructInsert(employee, "firstname", "Richard",1)>
<cfset StructInsert(employee, "lastname", "Burton",1)>
<cfoutput>
<!---Accessing a memebr of the structure--->
Full name of employee is: #employee.firstname# #employee.lastname#
</cfoutput>
A browser display of the above file appears as follows;
Binary and Object type category of variables
The examples for these will be deferred until we get used to more CFML tags, such as <cffile>, <cfobject> and so on.