|
ColdFusion Home
Basic ColdFusionIntroductionColdFusion Installation ColdFusion Editors ColdFusion Syntax ColdFusion Includes ColdFusion Variables ColdFusion Variable Types ColdFusion If Statements ColdFusion Loops ColdFusion Datasource ColdFusion Database Queries ColdFusion Lists ColdFusion Arrays ColdFusion Redirect ColdFusion Debugging ColdFusion Error Handling Advanced ColdFusionColdFusion MailColdFusion Functions ColdFusion Components ColdFusion Web Services ColdFusion Upload File ColdFusion Read File ColdFusion Write File ColdFusion Append File ColdFusion Rename File ColdFusion Copy File ColdFusion Move File ColdFusion Delete File Cffile Parameters Using cffile Parameters ColdFusion FTP (cfftp) cfftp Cached Connections ColdFusion HTTP (cfhttp) ColdFusion Query of Queries ColdFusion Charts ColdFusion Summary ColdFusion AdminCF AdministratorCF Archive and Deploy CF Scheduled Tasks CF Mini Tutorial ColdFusion BooksColdFusion MX BibleMacromedia ColdFusion MX 7 Web Application Construction Kit Got a MySpace Page?Get "www.yourname.com" for your MySpace page. Learn how >>. |
ColdFusion VariablesVariables are a standard part of any programming language. A variable can be visualised as a container that stores a value. We can use variables in many circumstances. For example, we could store the user's name inside a variable. We could then present the user's on the web page they're visiting. We could also perform a test against the variable to see what the value is. The application could then perform a different action depending on the value of the variable. Using cfsetSyntaxTo set a ColdFusion variable, use the cfset tag. To output the variable, you need to surround the variable name with hash (#) symbols and enclose it within cfoutput tags.
Example of UsageThis example uses the cfset tag to declare a variable called "firstname" and assign a value of "bono" to it. It then outputs the contents of the variable. ColdFusion code:
Display in browser:
Hello Bono.
Using cfparamThe cfparam tag creates a variable if it doesn't already exist. You can assign a default value using the default attribute. This can be handy if you want to create a variable, but don't want to overwrite it if it has already been created elsewhere. Example 1In this example, the variable hasn't been set previously, so it will be assigned with the cfparam tag.
This results in the following:
Hello Ozzy
Example 2In this example, the variable has already been assigned (using the cfset tag), so this value will override the default value in the cfparam tag.
This results in the following:
Hello Barney
Checking if a Variable ExistsYou can check if a variable has been defined or not by using ColdFusion's built in IsDefined() function. This can be used inside a cfif tag to prevent nasty error messages in the event you attempt to refer to a variable that doesn't exists. You could also use this function to determine whether a user has performed a certain action or not.
Enjoy this website?
Oh, and thank you for supporting Quackit! |
Featured Template:
(Build your websites in minutes!) |