coldfusion: variables

 

Understanding Variables

A variable is "a quantity capable of assuming any of a set of values."

As mentioned last year, you can think of a variable as a container that holds information. You create variables by giving them a name and assigning them some value.

The Cold Fusion Variable is very similar to the FLASH OR jAVASCRIPT Variable.

  • A Variable allows you to store and process information retrieved from a source: (either: User Input, Database Query, Data Syndication, or Time Condition)
  • Variables can be manipulated using FUNCTIONS built into the Application server.
  • Variables have SCOPES to help define them.

ColdFusion Server uses server memory to store these value until either you call for them by using the variable name or they are no longer needed, in which case they are deleted.

Two broad categories of variables are used in ColdFusion:

  1. simple variables and
  2. complex variables.

Complex varables will be discussed in Step 10, "Using Lists, Arrays, and Structures."

Data Types

ColdFusion variables can hold many different data types. A data type indicates what kind of information is being stored.

ColdFusion variables can store many data types.

Data type Description
These are whole numbers (numbers without anything to the right of the decimal point), such as 0, -17, and 105.
Real numbers, also referred to as floating-point numbers, are numbers that might include a decimal value, such as 3.17, -0.175, and 25.387.
Strings are a sequence of symbols, such as letters, numbers, and special characters.
Strings are enclosed by either single or double quotes. For example "ColdFUSion Mentor", 'user@anysite.com',and "10".

A Boolean value is either True or False. For example, <CFSET UserIsLOggedIn = True>.

Boolean values can be expressed in a number of ways. Negative values can be False, No, or 0. Positive vales can be True, Yes, or 1 (or any nonzero number).

Date-time values can be date only, time only, or a combination of both the date and the time, such as the result of the Now( ) function.
A list is a string that consists of multiple entries separated by some type of delimiter. The comma is the default defimiter, but others can II be specified. Lists will be explained in Step 10. An example might be <CFSET Colors="Red, White, Blue">.
This complex data type stores information in a table-like structure of rows and columns Arrays are be explained in Step 10.
This complex data type stores information in a series of key-value pairs. Structures will be explained in Step 10.
This complex data type holds the results of a database query.
Binary data is raw data, such as the contents of a file or an executable program
These are complex object types that are created using the < CFOBJECT> tag. They can include things like COM, Java, and CORBA objects.

ColdFusion variables are typeless, meaning you are not required to assign a specific data type to a variable name. ColdFusion automatically evaluates variable values when they are used in operations to determine how the variable should used.

Because coldfusion variables are typeless, there can be some confusion about the value of some strings. For example, the code
<CFSET Age="30"> set the value of the Age variable to the string value of "30" (note the quotation marks).

ColdFusion uses what is called operation-driven evaluation; this means that when ColdFusion sees a mathematical operators, such as subtraction or multiplication, it automatically tries to convert all the operands (elements in the equation) into numbers.

So, in ColdFusion, even though the Age variable contains a string, instead of throwing an error, the code
< CFSET Age="30">
< CFSET YearsOverTheHi11= Age - 21>
< CFOUTPUT>#YearsOverTheHi11#</CFOUTPUT> would output the numerical value of 9.

Conversely, if you use the following code to set the DaysTillXmas variable to the integer volt 30 (note that there are no quotation marks), it will be treated as a number.
< CFSET DaysTillXmas=30>
If we then try to combine it with text (as seen in the following code), ColdFusion will be smart enough to convert it to the string value of "30" rather than the number. For example, the code

<CFSET DaysTillXmas=30>
<CFSET Message= "Only #DaysTillXmas# to go!">
< CFOUTPUT>Hey kids, Santa's coming. #Message# </CFOUTPUT>

would output Hey kids, Santa's coming. Only 30 days to go!

There might be times when you want to make sure that a variable gets evaluated as a number and not a string or vice versa.

A couple of ColdFusion functions can help you do this:

The Val ( ) function will evaluate a string into a number (if possible). For example, the following line of code would convert the Age variable into a number: <CFOUTPUT>This is the number #Val(Age)#</CFOUTPUT>

To convert a number into a string, you can use ToString ( ). For example, the following line of code converts an integer value of 30 into the string "30".
<CFOUTPUT>This is the string #ToString(30)#</CFOUTPUT>

Variables not only contain various data type, they also have varying scopes