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.
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:
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.
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 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 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> 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". Variables not only contain various data type, they also have varying scopes |
|||||||||||||||||||||||||||