Home arrow ColdFusion arrow Page 3 - Everything You Must Know About ColdFusion Variables
COLDFUSION

Everything You Must Know About ColdFusion Variables


Understanding ColdFusion variables and logic is vital to being able to program in the language. In this tutorial, Jay's second covering ColdFusion, you will learn about the types of ColdFusion variables and how to use them.

Author Info:
By: Jayaram Krishnaswamy
Rating: 4 stars4 stars4 stars4 stars4 stars / 25
August 15, 2005
TABLE OF CONTENTS:
  1. · Everything You Must Know About ColdFusion Variables
  2. · Developing ColdFusion Content
  3. · Variable definition with CFSET tag
  4. · Variable by assigning statements in CFSCRIPT
  5. · Scope of a variable
  6. · Data Type Category of Variables
  7. · How do we name a variable?

print this article
SEARCH DEVARTICLES

Everything You Must Know About ColdFusion Variables - Variable definition with CFSET tag
(Page 3 of 7 )

In the earlier article the <cfset> tag was introduced. This tag sets the value of a variable. Trying to use a variable before it is assigned a value results in an error. The best practice is to test its existence before using it. Here is an example. This file undefined.cfm would produce an error, since the variable jac is not defined as shown in the picture below the code. As indicated in the error message, a more robust error message can be generated after changing the settings in the ColdFusion Administrator. We postpone this for later.

Syntax
<CFSET variable_name=expression>

<cfoutput>#jac#</cfoutput>

Variable assignment with <cfparam> tag

The above problem could have been identified before calling the variable, by using some kind of logic. However, there is yet another way of avoiding the issue. This is by using the <cfparam> tag to define the variable. In fact, <cfparam> can not only be used in the context of a variable, but can also be used in a much wider range of variables, including simple data types as well as complex data types. Here is an example of its usage.

Syntax
<CFPARAM NAME="param_name"
TYPE="data_type"
DEFAULT="value">
<!---Variable definition using CFPARAM tag--->
<!---Usage of <cfparam>--->
<!---If named parameter does not exist, 
it returns the default specified---> <cfparam name="jac" default="Okidoki"> <cfoutput> Variable 'jac' is not defined<br/> </cfoutput> <!---<cfset jac="Jay">---> <cfoutput> My name is #jac#. </cfoutput> <hr> <cfparam name="jac" default="Okidoki"> <cfoutput> Variable 'jac' is defined as 'Jay'<br/> </cfoutput> <cfset jac="Jay"> <cfoutput> My name is #jac#. </cfoutput>

Now you look at the browser output when this file cfparam.cfm is browsed. In the first part, a variable was not defined and therefore the display was a default value. In the second part, a variable did exist and was defined. In this case the value of the variable was displayed.


blog comments powered by Disqus
COLDFUSION ARTICLES

- Adobe ColdFusion Just Got More RAD
- How to Access a SQL Anywhere Database with C...
- CFXML: Probing XMLDOM in ColdFusion
- Creating a Web Service with ColdFusion: the ...
- CFAjax: What it is and How to Use it
- Querying SQL 2000 Server from ColdFusion
- Introduction to ColdFusion Markup Language, ...
- Introduction to ColdFusion Markup Language
- Databases and Dreamweaver MX 2004, concluded
- Databases and Dreamweaver MX 2004
- Welcome to Coldfusion MX 6.1, concluded
- Welcome to Coldfusion MX 6.1
- What You Must Know About ColdFusion Flow-Con...
- What You Must Know About Operators in ColdFu...
- Everything You Must Know About ColdFusion Va...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials