Programming ColdFusion 101 – Basic Concepts: Variables and Data Types

Share Button

Variables and Data TypesThis is the third installment of a 6 part series. All sections will help to introduce you to some basic programming concepts that will all carry through and help you as you learn ColdFusion. If you are looking for any of the other installments to this series, you can get to them below.

  1. What is Programming?
  2. Proper Syntax
  3. Variables and Data Types
  4. Conditional Processing
  5. Arrays and Structures
  6. Modular Code

Variables and Data Types

If you’ve never heard of a variable before, get ready to have them drilled into your brain! But what exactly is a variable? A variable is simply a way to store a value to use later on in your code. They are a major part of any programming language and are used in a variety of ways. For example, let’s go back to my famous red hat.

Imagine you came to my website and when you first entered, there was a form asking you what color your hat was. You obviously love the color red like I do, so that’s what you entered.

Variables and Data Types

Upon clicking submit, I could take the value that you typed into that textbox, that I just so happened to name “thishat” and save that value for later. My variable called “thishat” now has a value of “Red”. Later on, you might come to a section of my website where I display awesome hats based on your color preference. To find out what color you prefer, I could grab the value of the variable called “thishat” that I created when you first entered and use that to display only hats of your color choice.

What About Data Types?

Every variable you use in ColdFusion can hold a different type of data. These data types could be numbers, plain text strings, date/time values, Boolean values or one of many other data types.

Full List of Data Types

  • Integers – Whole numbers without anything to the right of the decimal
  • Real Numbers – Numbers that might include a decimal value
  • Strings – Sequence of letters or special characters enclosed by quotes
  • Booleans – A true or false value that can be expressed with True/False or 1/0 values
  • Date/Time – Can be date, time or date/time combination values
  • List – A delimiter separated list of values
  • Array – Complex table like structure with rows and columns
  • Structures – A series of name/value pairs
  • Queries – The result set returned from a database
  • Binary – Raw data
  • Object – Holds advanced data from items such as COM or Java objects

Well, that’s a lot of boring and technical chatter isn’t it? If you think so too, there is good news! In other programming languages, you have to tell your code what kind of data you are sending to it. In other words, if you want to use a variable called “thishat” and have the value as “red”, you would need to tell your code (declare) that you are using a “string” type variable.

Now that you understand what a data type is, I can tell you that for the most part, you won’t worry about telling ColdFusion about the data types too much. With ColdFusion, variables are typeless. This means you simply set the value and you are free to use it how you wish. ColdFusion is designed to evaluate your variables and will automatically determine how it should be used. Of course if you try to use the variable in a way that doesn’t make sense, such as trying to do math on a string, ColdFusion will tell you it doesn’t understand by throwing an error.

Where data types do come into play is when you are checking for a certain data type in ColdFusion. For example, let’s assume you have created a form on your website and one of the fields you have your user fill out is a field called “yearborn”. You’ve also added a column in your database called yearborn with a data type of integer. If your user filled out the value as “nineteen eighty” instead of 1980, how do you think your database would react to that? Using ColdFusion you can check your data types before doing any processing in a database and handle it with finesse.

Variable Output

Once you have a variable and a value stored inside it, you may want to output the value of the variable onto the screen. To do that, we simply need to surround the variable name with pound symbols and place that within a <cfoutput></cfoutput> tag set. Pound symbols are what ColdFusion uses to tell the application “hey I’m a piece of dynamic content, be sure to process me”. So if I wanted to show you the value of “thishat” variable onto the screen:

My hat is: <cfoutput>#thishat#</cfoutput>

The result on screen would simply be:

My hat is: Red

In the next section we will talk about how we can process a page based on certain conditions, so when you’re ready let’s move on.


Leave a Reply

Your email address will not be published. Required fields are marked *