Using ColdFusion Variables
Senior Programmer, Webhanger.com
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">
5. <title>My second ColdFusion Template</title>
Is normal HTML, this should all be familiar, it's normal HTML. . . . . .
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.
REMOTE_ADDR is the IP address of the client requesting the template.
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 VariablesTo 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">
4 <title>My Third ColdFusion Template</title>
7 <cfif isDefined(variablename)>
8 <& .CF CODE THAT USES THAT VARIABLE& .>
10 <CFLOCATION URL="firstpage.cfm">
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.