Skip to content

Instantly share code, notes, and snippets.

@sipacate
Created September 17, 2012 19:53
Show Gist options
  • Save sipacate/3739388 to your computer and use it in GitHub Desktop.
Save sipacate/3739388 to your computer and use it in GitHub Desktop.
Basics

Programming is all about doing stuff to things. Generally, the stuff is the execution of a process or algorithm and the things are information or data.

An algorithm is just a fancy name for a series of steps, like tying your shoelaces. Information or data is just values or information. Your name is a piece of information and so is your birth date. Since it's shorter, in this article we'll use the word data to describe a piece of information.

In ColdFusion, data is held in variables. Think of a variable like a mailbox. You can stuff things like letters and packages into a mailbox and get them out later to do stuff. ColdFusion makes storing information very easy because it's a loosely typed Language. You can stick any kind of data into a ColdFusion variable without having to tell ColdFusion what kind of data it is.

Here's the simplest code to set a variable:

<cfset ThisIs = "fun">

Put the above line of code into your application, save it and run the page.

Now, any time I refer to ThisIs in my ColdFusion code, it'll hold the data "fun". You can look at the contents of a variable in ColdFusion by using the CFDump command.

<cfdump var="#ThisIs#">

Put the above line of code into your application, save it and run the page.

See how the variable contains the string "fun"?

A few things to note. Examine the statement again:

<cfset ThisIs = "fun">

The part of the statement to the left (ThisIs) of the equals sign is the variable name. The part of the statement to the right of the equals sign ("fun") is the value to assign to the variable.

The Left Side Of The Statement <cfset ThisIs

The left side of the statement is the variable name. You can have numbers as part of the variable name, but the variable name must start with a letter. You must not have any spaces in your variable names. If you need to use more words for your variable name, you can simply write the variable with CamelCase letters, or use Under_Scores to separate each word if you want. The choice is one of style, there are no right answers. Also, most special characters are not allowed in variable names. A good rule to follow is variable names should be descriptive and help provide context to what is being done.

For example, the word variable is a bad naming choice because the name adds no context to why the variable exists:

<cfset variable = "12/26/1975">

The variable name UserBirthdate is a good naming choice because it add context to why the variable exists:

<cfset UserBirthdate = "12/26/1975">

Pro Tip: You'll understand the most about WHY your program is written a certain way as you are writing it. Take advantage of that hard-earned understanding and leave yourself (or others) as many clues and as much context as possible. Later on, after you've forgotten some of the details, it'll be much easier to piece together why the program was written a certain way and it'll be quicker to make good updates to your program.

The Right Side Of The Statement "fun">

The right side of the statement contains the value for the variable. Simple strings are enclosed in single or double quotes, with double quotes being the most common.

The right side of the statement is an execution zone. This means ColdFusion will attempt to evaluate items on the right side of the statement.

Examples

Print Out the Current Date:

<cfset DateToday = now()>
<cfdump var="#DateToday#">

Put the above lines of code into your application, save it and run the page.

See how the ColdFusion function now() was evaluated and the contents placed in the variable DateToday? Any thing not specifically in quotes (double or single) will be evaluated. It is possible to evaluate items in quotes, if you use the # sign.

Alternate Method: Print Out the Current Date:

This code will evaluate to the same thing in the previous step.

<cfset DateToday = "#now()#">
<cfdump var="#DateToday#"> 

However, unless there are strings in the quotes, it's generally preferred to use the previous method. If for some reason, you want to mix execution and strings, you can do so like this:

Mix Execution and Strings

<cfset DateToday = "Today is: #now()#">
<cfdump var="#DateToday#"> 

Concatenation

You could also concatenate the strings using the & operator. This example is functionally equal to the previous example:

<cfset DateToday = "Today is: " & now()>
<cfdump var="#DateToday#"> 

So is this one:

<cfset DateToday = "Today is: ">
<cfset DateToday = DateToday & now()>
<cfdump var="#DateToday#"> 

See how we added DateToday to another evaluation and replaced the DateToday variable name with the new contents?

Outputting a Variable

There are a couple of reasons to output the contents of a variable. One reason would be to display the contents of a variable to the user, say to display the username on a web page. Another reason would be to verify the contents of a variable while you are in the process of writing or debugging your program.

About CFOUTPUT If you want to display the contents of a variable to a user, the CFOUPTUT Command is the right choice. The variable reference must be a simple value, like one that can be displayed as text. This includes Strings, Numbers, Dates, Times and so on. Complex variables, like Structs, Arrays, Queries, Functions and so on, can not be displayed with the CFOUTPUT command because they are not displayable as text.

Example of CFOUTPUT Usage

<cfset DateToday = "Today is: #now()#">

<cfoutput>#DateToday#</cfoutput>

The variable will be evaluated, and the variable contents will be added to the current end of the response buffer.

About CFDUMP

If you want to inspect or verify the contents of a variable, the CFDUMP command is the right choice. ColdFusion makes it very easy to see the contents of ANY variable by using the CFDUMP command. CFDUMP will convert the variable contents to a string representation and format it for easy viewing. The CFDUMP command can be used to help debug your program.

Example of CFDUMP Usage

<cfset DateToday = "Today is: #now()#">

<cfdump var="#DateToday#">


<cfset DateArray = [dateFormat(now(), "short"), dateFormat(now()+1, "short"), dateFormat(now()+2, "short")]>

<cfdump var="#DateArray#">

<cfset DateStruct = { today=dateFormat(now(), "short"), tomorrow=dateFormat(now()+1, "short"), later=dateFormat(now()+2, "short") } >

<cfdump var="#DateStruct#">
@atuttle
Copy link

atuttle commented Sep 18, 2012

Also, most special characters are also not allowed

double use of "also".

In the final section, Outputting a Variable, I might go into a little more detail about when to use cfoutput vs. cfdump. As written it sounds like they are equally viable options for regular output, but dump spits out lots of extra JS/etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment