ColdFusion Variables CF uses variables to store data in memory. There are many different types of...
-
date post
21-Dec-2015 -
Category
Documents
-
view
220 -
download
0
Transcript of ColdFusion Variables CF uses variables to store data in memory. There are many different types of...
ColdFusion Variables
• CF uses variables to store data in memory.
• There are many different types of variables; each has its own use.
• To use a variable, name it and assign it a value.
Typeless
• All CF variables are typeless, which means you do not need to identify the type of the data(Such as numbers or dates.)
• Behind the scenes CF stores all the values as strings.
• When variables are accessed and expected to be of a certain type they are “cast” into the expected type.
Variable Naming
• Rules– Use one word– Do not use spaces– Begin with a letter– Use only letter, and the underscore– Do not use special characters
Variable Prefixes
• There are many types of variables in CF.
• Each variable has it is own scope– Where it exists– How long it exist for– Where the variable value is stored.
Referencing Variables
• To reference a local variable’s value, use the variables prefix.
• Notice that you reference variables in the expression (right hand side of the equal sign) without using double quotes.
<cfset firstname = “Sue”><cfset lastname=“Smith”><cfset fullname = variables.firstname +
variables.lastname>
CF would attempt to convert FirstName and LastName to numeric values which would result in an error.
<cfset thenum=“40”>
<cfset newnum = variables.thenum+”25”>
<cfset the string = “new number is: variables.newnum>
Creating local variables with <CFSET>
• The <cfset> tag is used to initialize the value of a variable.
• You set the value as being equal to an expression.
• The syntax for this tag is:
<cfset variable_name = expression>
Variables have specific values
<cfset firstname= “Sue”>
<cfset age= “29”>
• All literal values are surrounded by double quotes “Sue” “29”
&
• Here, variable draws its value from two other variables:
<cfset firstname = “Sue”>
<cfset lastname=“Smith”>
<cfset fullname= variables.firstname &” ”& variables.lastname>
&
• The ampersand (&) is a concatenation character and will build the string with the value of FirstName, a space and the value of last name.
• The result would be:
<cfoutput>#variables.fullname#</cfoutput>
• Sue Smith
<CFQUERY>
CFQUERY
CF Functions
• CFML is made up of two primary language elements– Tags – perform operations such as accessing a
database, evaluating a condition
– Functions- Return (possibly process) data and do things such as getting the current date and time, converting text to uppercase, and rounding a number to its nearest integer.
• #Now()# is an instruction telling CF to execute a function named Now()- a function that returns the current date and time.
• What happens – If no pound signs?– If put outside the cfoutput tag?
• Format of that date function is not entirely readable.
• Another function, DateFormat( ) can help here.
• DateFormat () role is to format dates so they are readable.
• DateFormat is an example of a function that accepts (and requires) that data must be passed to it – after all it needs to know which date you want to format to display.
• #DateFormat(Now())# tells CF to format the date returned by the Now() function.
NESTed
• Passing a function as a parameter to another function is referred to as NESTING.
• The Now() function is said to be NESTED in the DateFormat() function.
DateFormat()
• DateFormat() takes second optional attribute: – A format mask used to describe the output format.
#DateFormat(Now(), “MMMM-DD-YY”)#
#DateFormat(Now(), “MM-DD-YY”)#
#DateFormat(Now(), “DDD,MMMM DD-YYYY”)#
Parameters passed to a function are always seperated by comma.
RecordCount
RecordCount contains the total number of records returned by the query.
Number of rows returned:
<cfoutput>
#queryname.recordcount#
</cfoutput>
CurrentRow
<cfoutput query =“queryname”>
Row#queryname.currentrow# of #queryname.recordcount#
</cfoutput>