Understanding Variables

I l @ ve RuBoard

Understanding Variables

Variables are the bread and butter of ColdFusion. They are used to store information such as names , products, values, information from a form, numbers , dates, times, and any other pieces of data that you will need to use later on. There's really no limit to what you can store in a variable. The information in a variable can be accessed any time it is needed. The variable is stored in memory, and when it is referenced by its name , ColdFusion retrieves that variable and outputs it, uses it in a function, or whatever you have specified that it do.

There are several types of variables in ColdFusion. All variables are stored in memory for as long as they are needed. Some are available on multiple templates and are stored in memory or a database or cookie in between requests ; others persist for one request and then are removed from memory. Each different type serves its own purpose.

A local variable is one that is created within or passed to a template and exists only for that template. As soon as ColdFusion is done processing the template, the variable is gone and is no longer available. One example of a local variable is a form variable ”one that is passed when a form is submitted. Another example of a local variable is a URL variable ”one that is passed from one template to another through the URL. The final local variable is one that is created on the same template it is used on. This and a URL variable are the ones you will be working with in this lesson.

Most of the variables you will encounter as you begin learning ColdFusion will be local variables. For instance, if you wanted to display the date on a page, you would create a variable that holds the date variable. You could use that variable to display the date as many times as you wanted on that page. However, once the page finished loading, you would no longer be able to use the variable. Because the date variable is created each time the page is loaded, you would always be assured that your date is correct.

Session and Application variables are stored in the memory of the server and persist for a given amount of time. When the variable "times out" it is erased from memory and no longer available. These variables can be used on any template as long as they are still in memory. However, application variables are global and can be accessed by any user, whereas session variables are unique to one user's session and can be accessed only by that user. You would use these types of variables when you wanted to track something over multiple requests. Unlike the local variables, when you create a session variable, it can be accessed and output over a series of pages. It is no longer available once the session times out. You can use these variables to track a user 's login status or to track items in users' shopping carts. You would use an application variable to track a value that was universal for the entire application. You could set an application variable that contained your data source name, and reference this variable instead of the actual data source name. That way, if you ever change your data source name, you would only have to update it in one location. Once you changed the application variable, all of your code would function properly.

Client variables are similar to session variables in that they are unique to one user or client, but they are stored differently. Client variables can be stored in one of three places: in the server registry, as a cookie on the user's machine, or in a database on the ColdFusion server. The third option is the best choice, due to limitations with cookies and the registry. Client variables can be used almost identically to session variables, with the exception that client variables cannot store complex variables like arrays or structure. Otherwise you can use them to track values such as a login status, a user's ID, or any other value you need to reference on multiple templates.

In this chapter, we'll concentrate only on local variables, but it's important to have a basic understanding of the other types as well.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net