Define a ColdFusion variable. If the variable exists, cfset
resets it to the specified value.
<cfset variable_name = expression>
cfcookie,
cfparam,
cfregistry,
cfsavecontent,
cfschedule
The following example assigns a new array to the variable "months".
<cfset months = ArrayNew(1)>
This example creates a variable "Array_Length" that resolves to the length of the array "Scores".
<cfset Array_Length = ArrayLen(Scores)>
This example assigns to index position two in the array "months" the value "February".
<cfset months[2] = "February">
In this example, the variable name is itself a variable.
<cfset myvariable = "current_value">
<cfset "#myvariable#" = 5>
In this example, a COM object is created. A cfset
defines a value for each method or property in the COM object interface. The last cfset
creates a variable to store the return value from the COM object's "SendMail" method.
<cfobject action = "Create"
name = "Mailer" class = "SMTPsvg.Mailer"> <cfset MAILER.FromName = form.fromname> <cfset MAILER.RemoteHost = RemoteHost> <cfset MAILER.FromAddress = form.fromemail> <cfset MAILER.AddRecipient("form.fromname", "form.fromemail")> <cfset MAILER.Subject = "Testing cfobject"> <cfset MAILER.BodyText = "form.msgbody"> <cfset Mailer.SMTPLog = "logfile"> <cfset success = MAILER.SendMail()> <cfoutput> #success# </cfoutput>
<!--- This example shows how to use cfset ---> <cfquery name = "GetMessages" dataSource = "cfsnippets"> SELECT * FROM Messages </cfquery> <html> <head> <title> cfset Example </title> </head> <body bgcolor = silver> <H3>cfset Example</H3> <P>cfset allows you to set and reassign values to local or global variables within a CF template. <cfset NumRecords = GetMessages.recordCount> <P>For example, the variable NumRecords has been declared on this template to hold the amount of records returned from our query (<cfoutput>#NumRecords#</cfoutput>). <P>In addition, cfset can be used to pass variables from other pages, such as this example which takes the url parameter Test from this link (<a href = "cfset.cfm?test = <cfoutput>#URLEncodedFormat(" hey, you, get off of my cloud")#</cfoutput>">click here</A>) to display a message: <P><cfif IsDefined ("url.test") is "True"> <cfoutput><B><I>#url.test#</I></B></cfoutput> <cfelse> <H3>The variable url.test has not been passed from another page.</H3> </cfif> <P>Finally, cfset can also be used to collect environmental variables, such as the time, the IP address of the user, or any other function or expression possible in ColdFusion. <cfset the_date = #DateFormat(Now())# & " " & #TimeFormat(Now())#> <cfset user_ip = CGI.REMOTE_ADDR> <cfset complex_expr = (23 MOD 12) * 3> <cfset str_example = Reverse(Left(GetMessages.body, 35))> ...