Accessibility
 
Home > Products > ColdFusion > Support > Getting Started with ColdFusion
ColdFusion Icon Macromedia ColdFusion Support Center - Getting Started with ColdFusion
Using pound signs to denote variables and functions

When the ColdFusion Server encounters a pound sign in a CFML statement, it assumes that a variable or a function call will immediately follow it. A trailing pound sign indicates the end of the variable name or function call. ColdFusion examines the text between the leading and trailing pound signs. If the text represents a variable, it substitutes the variable value for the text. If the text represents a function, Coldfusion executes the function and substitutes the return value of the function for the text. For example:

To send the current value of a variable named variables . my_first_name to the client, you surround the variable name with pound signs:
<cfoutput> Hello #variables.my_first_name# </cfoutput>
In this example, the variable #variables.my_first_name# is replaced with the current value assigned to it (for example, Mary Jones).
To send the current value of the Now function to the client, you surround the function name with pound signs:
<cfoutput> It is now #Now()#</cfoutput>
In this example, the Now function is replaced with result of the Now function which is the value of the current date and time.

Omitting the pound signs surrounding the variable or function in either of the previous examples would result in sending literal text to the client. ColdFusion requires pound signs to evaluate text only inside strings; tag attributes; or cfoutput, cfquery, or cfmail tag bodies.

Note, however, ColdFusion does not require pound signs to evaluate text inside a c fset tag, because a cfset tag does not have attributes, it takes an assignment expression. Similarly, you can, but should not, use pound signs in most functions.

 
Guidelines for using pound signs
Follow these guidelines when using pound signs:

Use pound signs to distinguish variables or functions from plain text.
Surround only a single variable or function in pound signs; for example, #Variables.myVar# or #Left (myString,position)#. However, a function in pound signs can contain nested functions, such as #Left(trim(myString), position)#.
Do not enclose complex expressions, such as 1 + 2, with pound signs.
Do not use pound signs when using the cfset tag to assign one variable's value to another value.
Use pound signs only where necessary, because unneeded pound signs slow processing.

 
Examples of using pound signs
You can use pound signs a number of ways in your CFML statements to flag variables or functions. The following sections describe some of the ways you can use them.

Using pound signs in tag attributes
You can put variables, functions, or expressions inside tag attributes by enclosing the variable or expression with pound signs.

Example Description
<cflocation url="tripdetail.cfm?ID=#Form.RecordID#">

The variable Form.recordID determines which record is shown on the cfm page.

<cfcookie name = "TestCookie" value="The value is #CookieValue#">

If the value for CookieValue was Chocolate Chip, the results of this statement would be:

The value is Chocolate Chip


Using pound signs in specific CFML tags
You can put variables or functions freely inside the bodies of the following tags by enclosing each variable or expression with pound signs.

Tag Example Description
cfoutput

<cfoutput>

"Mo" and "nk" is

#Left("Moon", 2)#

#Mid("Monkey", 3, 2)#

</cfoutput>

Two expressions inside pound signs can be adjacent to one another, as in this example.

This statement displays the following results:

"Mo" and "nk" is Monk

cfquery

<cfquery name="TripQuery" datasource="Trips" maxrows="1"

SELECT tripID

FROM trips

WHERE tripID = #Form.RecordID#

</cfquery>

Form field variables can be enclosed in pound signs and used in a database query.

cfmail

<cfmail

to= "webmaster@companyname.com"

from =

"#form.emailAddress#"

Comments received: #form.comment#

</cfmail>

Form field variables can be used in many Coldfusion tags including cfmail.


Using pound signs in strings
You can put variables or functions inside strings by enclosing each variable or expression with pound signs.

Example Description
<cfset TheString = " Hello, #FirstName#">

ColdFusion automatically replaces the text with the value of the variable.

<cfset TheString = "Hello, FirstName!">

<cfset TheString = "Hello, " & "FirstName!">

If you omit the pound signs, the text, rather than the value, appears in the results. In this example, the two cfset statements produce the same result (Hello, FirstName!).


To Table of Contents Forward to next document