<html>
<head>
<link href="mmres://user_interface_reference.css" rel="stylesheet" type="text/css">
<title>CFSET</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
   <div id="Description">
   <table cellpadding="0" cellspacing="0" border="0" width="100%" class="main">
      <tr> 
         <td valign="top" class="name">CFSET</td>
         <td valign="top" nowrap class="compatibility">&nbsp;</td>
      </tr>
      <tr>
         <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
      </tr>


    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">Description</span>
<p>
Sets a value in ColdFusion. Used to create a variable, if it does not exist, and assign it a value. Also used to call functions.
</p>
    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">Category</span>
<p>
Variable manipulation tags
</p>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="syntax"><span class="title">Syntax</span><pre>&lt;cfset 
   var variable_name = expression
   &gt;
</pre>    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">See also</span>
<p>
cfcookie, cfparam, cfregistry, cfsavecontent, cfschedule; Chapter&#160;2, "Elements of CFML," in ColdFusion MX Developer's Guide
</p>

<p>

</p>
    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">Usage</span>
<p>
The following sections provide detailed descriptions of some of the uses for the cfset tag.
</p>
    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">Calling functions</span>
<p>
When you use the cfset tag to call a function, you do not need to assign the function return value to a variable if the function does not return a value or you do not need to use the value returned by the function. For example, the following line is a valid ColdFusion cfset tag for deleting the MyVariable variable from the Application scope: 
</p>
<pre>&lt;cfset StructDelete(Application, &quot;MyVariable&quot;)&gt; 
</pre>    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">Arrays</span>
<p>
The following example assigns a new array to the variable months:
</p>
<pre>&lt;cfset months = ArrayNew(1)&gt;
</pre>
<p>
This example creates a variable Array_Length that resolves to the length of the array Scores:
</p>
<pre>&lt;cfset Array_Length = ArrayLen(Scores)&gt;
</pre>
<p>
This example assigns, to index position two in the array months, the value February:
</p>
<pre>&lt;cfset months[2] = &quot;February&quot;&gt;
</pre>    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">Dynamic variable names</span>
<p>
In this example, the variable name is itself a variable:
</p>
<pre>&lt;cfset myvariable = &quot;current_value&quot;&gt;
&lt;cfset &quot;#myvariable#&quot; = 5&gt;
</pre>    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">Function local variables</span>
<p>
The var keyword specifies that the variable being defined is only available inside the body of a function that you define using the cffunction tag. The variable value that is set in one invocation of the function is not available in any other invocation of the function. The var keyword is the equivalent of the var statement in CFScript. The following rules apply to the var keyword:
</p>
<ul>

<li>Any cfset tag that uses the var keyword must be inside the body of a cffunction tag. If you use the var keyword in a cfset tag outside a cffunction tag body, ColdFusion displays an error message. </li>

<li>You must place all cfset tags that use the var keyword at the beginning of the cffunction tag body, before any other ColdFusion tags. </li>
</ul>

<p>
The following example shows how to use the new keyword:
</p>
<pre>&lt;cffunction name=&quot;myFunct&quot;&gt;
   &lt;cfset var myVar = &quot;This is a test&quot;&gt;
   &lt;cfreturn myVar &amp; &quot; Message.&quot;&gt;
&lt;/cffunction&gt;
&lt;cfoutput&gt;#myFunct()#&lt;/cfoutput&gt;
</pre>
<p>
In this example, the variable myVar exists only when the function myFunct executes, and it is not available elsewhere on the ColdFusion page. 
</p>
    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">COM objects</span>
<p>
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.
</p>
<pre>&lt;cfobject action = &quot;Create&quot; 
   name = &quot;Mailer&quot; 
   class = &quot;SMTPsvg.Mailer&quot;&gt; 

&lt;cfset MAILER.FromName = form.fromname&gt; 
&lt;cfset MAILER.RemoteHost = RemoteHost&gt; 
&lt;cfset MAILER.FromAddress = form.fromemail&gt; 
&lt;cfset MAILER.AddRecipient(&quot;form.fromname&quot;, &quot;form.fromemail&quot;)&gt; 
&lt;cfset MAILER.Subject = &quot;Testing cfobject&quot;&gt; 
&lt;cfset MAILER.BodyText = &quot;form.msgbody&quot;&gt; 
&lt;cfset Mailer.SMTPLog = &quot;logfile&quot;&gt; 
&lt;cfset success = MAILER.SendMail()&gt; 
&lt;cfoutput&gt; #success# &lt;/cfoutput&gt;
</pre>    </td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" colspan="2" class="description"><span class="title">Example</span><pre>&lt;!--- This example shows how to use cfset. ---&gt;
&lt;cfquery name = &quot;GetMessages&quot; dataSource = &quot;cfdocexamples&quot;&gt;
   SELECT *
   FROM   Messages
&lt;/cfquery&gt;

&lt;h3&gt;cfset Example&lt;/h3&gt;
&lt;p&gt;cfset sets and reassigns values to local or global variables within a page.

&lt;cfset NumRecords = GetMessages.recordCount&gt;
&lt;p&gt;For example, the variable NumRecords has been declared on this 
page to hold the number of records returned from query
(&lt;cfoutput&gt;#NumRecords#&lt;/cfoutput&gt;).

&lt;p&gt;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: 
(&lt;a href = &quot;cfset.cfm?test = &lt;cfoutput&gt;
                  #URLEncodedFormat(&quot;hey, you, get off of my cloud&quot;)#               &lt;/cfoutput&gt;
               &quot;&gt;click here&lt;/A&gt;) to display a message: 
&lt;p&gt;
&lt;cfif IsDefined (&quot;url.test&quot;) is &quot;True&quot;&gt;
   &lt;cfoutput&gt;&lt;b&gt;&lt;I&gt;#url.test#&lt;/i&gt;&lt;/b&gt;&lt;/cfoutput&gt;
&lt;cfelse&gt;
   &lt;h3&gt;The variable url.test has not been passed from another page.&lt;/h3&gt;
&lt;/cfif&gt;

&lt;p&gt;cfset can also be used to collect environmental variables, such as the 
time, the IP address of the user, or another function or expression.

&lt;cfset the_date = #DateFormat(Now())# &amp; &quot; &quot; &amp; #TimeFormat(Now())#&gt;
&lt;cfset user_ip = CGI.REMOTE_ADDR&gt;
&lt;cfset complex_expr = (23 MOD 12) * 3&gt;
&lt;cfset str_example = Reverse(Left(GetMessages.body, 35))&gt;

&lt;cfoutput&gt; 
   &lt;ul&gt; 
      &lt;li&gt;The date: #the_date# 
      &lt;li&gt;User IP Address: #user_ip# 
      &lt;li&gt;Complex Expression ((23 MOD 12) * 3): #complex_expr# 
      &lt;li&gt;String Manipulation (the first 35 characters of 
            the body of the first message in our query) 
            &lt;br&gt;&lt;b&gt;Reversed&lt;/b&gt;: #str_example# 
            &lt;br&gt;&lt;b&gt;Normal&lt;/b&gt;: #Reverse(str_example)# 
   &lt;/ul&gt; 
&lt;/cfoutput&gt; 
</pre>
         </td>
      </tr>
   </table>
   </div>
<div id="VAR">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>  
<td valign="top" class="name">VAR</td>
  <td valign="top" nowrap class="compatibility">&nbsp;</td>
  </tr>
  <tr>
  <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
  </tr>

  <tr>
  <td valign="top" class="syntax">&nbsp;</td>
  <td valign="top" nowrap class="requirements">Optional</td>
  </tr>
  <tr>
  <td colspan="2" class="clearseparation">&nbsp;</td>
  </tr>
  <tr>
  <td valign="top" colspan="2" class="description">



<p>A keyword. Does not take a value. Identifies the variable as being local to a function. The variable only exists for the time of the current invocation of the function.</p>

  </td>
  </tr>
  </table>
</div>
<div id="VARIABLE_NAME">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>  
<td valign="top" class="name">VARIABLE_NAME</td>
  <td valign="top" nowrap class="compatibility">&nbsp;</td>
  </tr>
  <tr>
  <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
  </tr>

  <tr>
  <td valign="top" class="syntax">&nbsp;</td>
  <td valign="top" nowrap class="requirements">Required</td>
  </tr>
  <tr>
  <td colspan="2" class="clearseparation">&nbsp;</td>
  </tr>
  <tr>
  <td valign="top" colspan="2" class="description">



<p>A variable.</p>

  </td>
  </tr>
  </table>
</div>

  </body>
</html>
