cfset
Description
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.
Categories
Related
Syntax
<cfset
var variable_name = expression>
Attributes
| Attribute | Description | Required | Default |
|---|---|---|---|
| var | 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. | Optional | |
| variable_name | A variable. | Required |
Usage
You use the cfset tag in several ways in your applications.
Example
<!--- This example shows how to use cfset. --->
<cfquery name = "GetMessages" dataSource = "cfdocexamples">
SELECT *
FROM Messages
</cfquery>
<h3>cfset Example</h3>
cfset sets and reassigns values to local or global variables within a page.
<cfset NumRecords = GetMessages.recordCount>
For example, the variable NumRecords has been declared on this
page to hold the number of records returned from query
(<cfoutput>#NumRecords#</cfoutput>).
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:
<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>
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>