By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,548 Members | 1,076 Online
Bytes IT Community

Using ColdFusion Variables

By Max Kington
Senior Programmer, Webhanger.com

Using Variables

We've seen what types of variables exist, now lets see how we use them. In ColdFusion code, variables come in the form of variabletype.variable. Wherever we want to have a variable value used in your code you have to use
#variabletype.variable#. Therefore if we wanted to output the browser information, the clients IP address we would do the following. Again, I'll do the code and explain it later.

1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2.
3. <html>
4. <head>
5. <title>My second ColdFusion Template</title>
6. </head>
7.
8. <body>
9. <cfoutput>#cgi.HTTP_USER_AGENT#</cfoutput><BR>
10. <cfoutput>#cgi.REMOTE_ADDR#</cfoutput>
11. </body>
12. </html>

Lines 1-8:

Is normal HTML, this should all be familiar, it's normal HTML. . . . . .

Line 9:

Is an example of a CGI variable in action. To transform a CF variable so that it is returned to the browser, the HTTP_USER_AGENT is the browser that the person is used. This could be used to define if a person is using a required browser for an action to be performed.

Line 10:

The REMOTE_ADDR is the IP address of the client requesting the template.

Lines 11-12:

Normal HTML.

Therefore, to output a variable, simply put it inside a <cfoutput> tag pair and use #variable#. However, this is not the only use for this foundation block of your ColdFusion applications.

Manipulating Variables

To be able to use variables effectively you have to be able to put values into them, you do this primarily thorough the <CFSET> tag. Therefore if you wanted to put a value into a variable, you would do the following, <CFSET foo = 'Value'> you could then use this variable somewhere else.

Handling Errors and Checking Variable availability.

The first thing that got me when I started learning from ColdFusion from having worked with Active Server Pages was the fact that ColdFusion will error if a variable has been used in a template and it hasn't yet been defined. For example, if you want to get some information from a database based on the link someone clicks on then you would use a URL variable. The problem is if someone goes to your page without following your link, i.e. bookmarking the output page, the URL variable won't be there and your template will error. There is a range of functions for you to check to see if your variables have been created and exist. The recommended function is the isDefined function. It returns true if the variable is there and false if it isn't. Hence you would use the following code to check to see if a variable existed and if not send the user back to the page where the variable will be defined.

1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <html>
3 <head>
4 <title>My Third ColdFusion Template</title>
5 </head>
6 <body>
7 <cfif isDefined(variablename)>
8 <& .CF CODE THAT USES THAT VARIABLE& .>
9 <cfelse>
10 <CFLOCATION URL="firstpage.cfm">
11 </cfif>
12 </body>
13 </html>

That piece of code is pretty self-explanatory and as you can see it'll only try to use the variable if variable exists, if it doesn't then send them back to a page which will allow them to use the variable. As you will see later on the is Defined function is very important and you will use it extensively as your ColdFusion applications increase in complexity. Now, back to variables. Now that we know what variables are, how to use them and how to check that they exist, let's do a real world example.

« Variables Image Gallery »