Jump to content
skaterdav85

using # sign in CF functions

Recommended Posts

I'm slightly confused on the use of # signs in functions. Typically in a CF tag I can do something like:

<cfset someVar = "#url.someURLVar#" />

or

<cfset someVar = url.someURLVar />

and both of these are equivalent.However, in the code below I must remove the the pound signs around url.testId. Why?

<cfif NOT IsDefined("url.testId")>	<cflocation url="index.cfm?fuseaction=app.existingTestList"></cfif>

Share this post


Link to post
Share on other sites
I'm slightly confused on the use of # signs in functions. Typically in a CF tag I can do something like:
<cfset someVar = "#url.someURLVar#" />

or

<cfset someVar = url.someURLVar />

and both of these are equivalent.However, in the code below I must remove the the pound signs around url.testId. Why?

<cfif NOT IsDefined("url.testId")>	<cflocation url="index.cfm?fuseaction=app.existingTestList"></cfif>

It is pretty tricky to put into words, so I hope my explanation is clear enough to help you understand.For the "isdefined" function, it is looking to see if the variable itself is defined. It is not worried about the value, just the fact that the variable exists.Lets say url.yourvar has somehow been set to "w3schools" if you code <cfif ISDEFINED("#url.yourvar#")> then you would essentially be asking ColdFusion to see if "w3schools" is defined. Well, "w3schools" is a value is not something that gets assigned a value. To check for "w3schools" you use <cfif url.yourvar IS "w3schools">.So, <cfif ISDEFINED("url.yourvar")> checks to see if an actual variable container exists - ColdFusion is looking to see if you've defined url.yourvar somewhere in the page request - its not to the point where its worried about the value assigned to it.One common use might be <cfif ISDEFINED("form.submit")> to check if a form was submitted or <cfif ISDEFINED("form.mycheckbox")> to see if a check box was selected in a form - since (if you don't know) a check box doesn't show up in the form scope if it is not checked.Does that make sense?

Share this post


Link to post
Share on other sites

ok that makes sense. But how come url.yourvar would need to be in quotes in the IsDefined function? Typically quotes represent a string, not a variable. My second attempt in debugging was:

<cfif IsDefined(url.yourvar)>

Share this post


Link to post
Share on other sites

The way ColdFusion generally translates things are as follows:a.) if it is in quotes, then ColdFusion uses the literal value - what you seeb.) if there are no quotes then it uses the evaluated value - what you don't seeso - (url.yourvar) is going to be evaluated and whatever url.yourvar is set to, i.e. w3schools, is used. Where ("url.yourvar") will be interpreted as the literal text, url.yourvar. Not like ("#url.yourvar#") will be the value, w3schools.Its just the way that function works. <cfif ISDEFINED(xxxx)> will error because of syntax, it needs the quotes to know what variable to look for in memory. What is in memory is the variable name and value - if it exists - and that's what this does, checks to see if it exists.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×