CFSET  
Description

Defines a ColdFusion variable. If the variable exists, this tag sets it to the specified value.

 
Category

Variable manipulation tags

 
Syntax
    <cfset 
   variable_name = expression>

  
 
See also

cfcookie, cfparam, cfregistry, cfsavecontent, cfschedule

 
Usage
 
Arrays

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">
 
Dynamic variable names

In this example, the variable name is itself a variable:

<cfset myvariable = "current_value">
<cfset "#myvariable#" = 5>
 
COM objects

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>

 
Example
<!--- This example shows how to use cfset --->
<cfquery name = "GetMessages" dataSource = "cfsnippets">
   SELECT  *
   FROM   Messages
</cfquery>

<h3>cfset Example</h3>
<p>cfset sets and reassigns values to local or global variables within a page.

<cfset NumRecords = GetMessages.recordCount>
<p>For example, the variable NumRecords has been declared on this 
page to hold the number of records returned from 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>cfset can also be used to collect environmental variables, such as the 
time, the IP address of the user, or another function or expression.

<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))>

<cfoutput> 
   <ul> 
      <li>The date: #the_date# 
      <li>User IP Address: #user_ip# 
      <li>Complex Expression ((23 MOD 12) * 3): #complex_expr# 
      <li>String Manipulation (the first 35 characters of 
            the body of the first message in our query) 
            <br><b>Reversed</b> :#str_example# 
            <br><b>Normal</b>: #Reverse(str_example)# 
   </ul> 
</cfoutput> 
VARIABLE_NAME  
  Required
 

Variable