ColdFusion Tutorial
Creating Coldfusion Variables
A variable are placeholders for actual values to be filled in later. The variables must have a name. In some other languages you have to specify what kind of variable it is, example: string, number, integer or text. You don?t have to do that in ColdFusion, because Coldfusion determines the variable type by the way it being used. That is one of the reasons why some programmers call Coldfusion a type less language.
Before we take a closer look at the naming rules of variables, we show you how to declare some ColdFusion variables.
Variables are created using <cfset> or <cfparam>.
First <cfset>
<cfset your_name = "Peter Pan">
This variable create a variable named your_name with a string value Peter Pan.
<cfset your_age = 49>
This variable create a variable named your_age with a numerical value of 49.
<cfparam>
<cfparam name="your_name" default="Harry Potter">
This variable creates a variable named your_name with the string value Harry Potter.
<cfset>
Variables created using <cfset>, does not check if the variable exits.
<cfparam>
Variables created using <cfparam> checks for the existence of the variable. The value of the variable being used in the template. If the variable does not exist and you have assigned a default value, ColdFusion create the variable and give it the default value.
Example:
Between the <BODY></BODY> tags, just as you would in HTML, enter the following code:
|
<CFSET your_name = "Peter Pan"> <CFOUTPUT>#your_name#</CFOUTPUT> <CFSET your_age = "49"> <CFOUTPUT>#your_age#</CFOUTPUT> |
Notice: The name of a variable must begin with a letter and can only contain letters, numbers and underscore. Using spaces, periods, commas, exclamation points, percents, ampersands etc. are not allowed in variable names.
ColdFusion Tutorial test
Creating Coldfusion Variables
No test is available or implemented for this ColdFusion Tutorial.
User Comments: 4
How to display pound sign:
You can have access to all symbols from the character map of windows and put them between (') or ("")
just like this:
<cfset Marys_variable = "This is an example of a string that contains a #'?'# pound sign.">
<cfoutput>#marys_variable#</cfoutput>
Adding pound signs in variables
I was searching for a way to output a field name with a pound sign in it in ColdFusion and came up empty. I'm actually referencing an Excel spreadsheet field that was given a name with a pound sign in it. The file is now set to read only so it was impossible for me to rename it.
Anyway, what I came up with was to ignore ColdFusion altogether, and reference the field name in SQL and rename it, and then output the new name in ColdFusion.
Here's what my code looks like. The field name is called "ABTL Group #".
<cfquery datasource="excel_test" name="excel_test">
select [ABTL Group ##] as GroupNo
from [Bill To$]
</cfquery>
<cfoutput query="excel_test">
#NumberFormat(GroupNo, "9999999")#
<br>
</cfoutput>
I'm not sure if this is what you were referring to with your earlier question. Anyway, I think it's a tricky way of getting around the pound sign issue in CF.
Cheers,
Peter
Hashmark in ColdFusion variable
Thanks Mary for your question. It helps improve this tutorial.
To insert hashmark (#) or pund sign in a string, you must escape the pound sign. This is important because they are also used to enclose CFML variables.
Example:
<cfset Marys_variable = "This is an example of a string that contains a ##pound sign.">
We will update the tutorial, both with pound, signsingle-quotation mark and double-quotation.
Regards
Webmaster
Hashmark in ColdFusion variable
What to do if I want a hashmark (#) sign in a variable?
Best regards
Mary
