Moving on with basic ColdFusion topics, today we'll look into the different variable types available. We'll also talk about the different ways of controlling the flow of the page through the use of conditional blocks and loops.

ColdFusion variable types

A really nice feature of ColdFusion is that it is a typeless language. What that means, in plain English, is that even though we declare a variable with a certain type, we can always come back later and assign it a different variable type later. However, the type of the variable will give us access to different functions that we'll be able to use later.

Simple types

The simple variable types are remarkable due to the fact that they're, well, simple. The best way to explain them is that they hold a simple value, as opposed to complex data.

String

String variables contain basic text. In our Hello world! example yesterday we stored our text in a string variable. When setting your string variable you need to enclose it within single- or double-quotes.

view plain print about
1<cfset myVariable = "myString" />
2
3<!--- is the same as --->
4<cfset myVariable = 'myString' />

I realize now I haven't talked about comments in ColdFusion. They're very simple, really. "<!---" opens a ColdFusion comment, and "--->" closes it. Whatever is encompassed within will not be parsed by ColdFusion, and will not be returned to the browser.

Numeric

This type of variable contains numbers. The only difference between numeric and string is that numerics can only contain numbers. ColdFusion can convert between types automatically as long as the value is a number. Numeric values do not need to be enclosed within quotes.

view plain print about
1<cfset myVariable = "23" />
2
3<!--- is the same as --->
4<cfset myVariable = 23 />

This variable type can also contain floating numbers (1.23, 8.234, etc).

Complex types

Complex variable types generally contain a collection of data.

Lists

A list is basically a string variable, however it is notable due to the fact that it may contain several values separated by a delimiter (the default is a comma (,)). Nothing special needs to be done for ColdFusion to recognize the variable as a list.

view plain print about
1<cfset myList = "boy,girl,mom,dad" />
2
3<!--- this could also be used as a list, although it only has one value --->
4<cfset myList = "superdog" />

As you can see, the list is defined the same way as a string. The only difference is the way we treat the variable.

Arrays

An array is basically a collection of variables. In its simplest form you'll see an array with a collection of simple variables:

view plain print about
1<!---
2    We create an array with the arrayNew() method.
3    It takes one argument, which is the number of dimensions.
4    For now, we'll just use 1 (it gets a little more complicated
5    the more dimensions you add.
6--->

7<cfset myArray = arrayNew(1) />
8
9<cfset myArray[1] = "this is my first value" />
10<cfset myArray[2] = 24 />

The way we store values within an array is through square brackets ([ and ]). Within them, we specify the position (also known as index) at which we want to save our value. It's important to know that the array must have a value at all the previous positions before you assign it to a new one. What that means is you can't assign a value to position 2 before assigning something to position 1.

What's really neat with arrays is that you can also store other complex variables within it:

view plain print about
1<cfset myArray = arrayNew(1) />
2
3<!---
4    Let's create our second array. We need to do it first
5    so we can place it in myArray later.
6--->

7<cfset myNestedArray = arrayNew(1) />
8<cfset myNestedArray[1] = "some value" />
9<cfset myNestedArray[2] = "some other value" />
10
11<cfset myArray[1] = "this is my first value" />
12<cfset myArray[2] = 24 />
13
14<!--- Place it in --->
15<cfset myArray[3] = myNestedArray />

If you want to display a value stored at a particular position in the index, all you need to do is use the same square brackets:

view plain print about
1<cfoutput>The value at position 2 is: #myArray[2]#</cfoutput>
The value at position 2 is: 24
Structures

A structure, in many ways, looks a lot like an array: it's designed to store a collection of data. It can also contain other complex variables. The main difference between the two is that structures don't use an index to store the position of the values. Instead, it uses keys.

A key is generally a string, although it can also be a numeric value, that is unique within the structure. This key reserves a spot for other values to be saved at. Where an array will always have indexes in sequence (ie.: 1, 2, 3, n... ), a structure's keys may have no apparent logic between them:

view plain print about
1<!---
2    We define the structure with the structNew() function.
3    As the name implies, it returns a new, blank structure.
4--->

5<cfset myStructure = structNew() />
6
7<!---
8    You can set a value within the structure by using square brackets.
9--->

10<cfset myStructure["color"] = "red" />
11<cfset myStructure["size"] = 12 />
12
13<!---
14    You can also use dot-notation. This implies using the structure's name,
15    then following it by a period and the name of the key.
16--->

17<cfset myStructure.favoriteMovie = "Scary Movie XII" />

Outputting data from a structure is as easy as with an array. In fact, it's actually more verbose: since you retrieve the value using a key rather than a numeric index, it gives a better idea as to what you are trying to return:

view plain print about
1<cfoutput>
2<!---
3    You can output a value from within a structure using either square
4    brackets or dot-notation. Notice that I don't need to retrieve the values
5    the same way they were set.
6--->

7My hair color is #myStructure.color# and my favorite movie is #myStructure["favoriteMovie"]#.
8</cfoutput>
My hair color is red and my favorite movie is Scary Movie XII.
Queries

Queries are a little more complicated, so we'll come back to them later. Basically, a query is a collection of data represented by rows. Generally this kind of data is retrieved from a database, although there are exceptions. A perfect example of a query is an Excel spreadsheet we're you'd have a list of the people in your little league and their information in columns. ColdFusion is really great for getting data out of databases and making it easy to handle it, so more on that in a couple of posts.

As you can see there are plenty of variable types out there. The trick is using the right one for the situation. I'll be coming back on these topics later and referencing back to this page in the following posts. We'll see the different things we can do with them and the best way to use them.