Highlighted

Difference in scope?

Explorer ,
Nov 07, 2016

Copy link to clipboard

Copied

Hi All,

What is the difference between #VARIABLES.ID# and #ID#?

Thanks!

John

Views

219

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Difference in scope?

Explorer ,
Nov 07, 2016

Copy link to clipboard

Copied

Hi All,

What is the difference between #VARIABLES.ID# and #ID#?

Thanks!

John

Views

220

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 07, 2016 0
Adobe Community Professional ,
Nov 07, 2016

Copy link to clipboard

Copied

The convention is that, on an ordinary Coldfusion page, ID is equivalent to VARIABLES.ID. Dump the variables scope, and you will see it.

<cfset x="A">

<cfset y="B">

<cfset id=x & y & "C">

<cfdump var="#variables#" label="Variables scope">

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 07, 2016 0
Advocate ,
Nov 09, 2016

Copy link to clipboard

Copied

It depends on where you are executing. On a stand-alone .cfm page, "usually" variables.ID and ID both refer to the same variable. But there are a lot of gotchas, especially when coding within functions as arguments and local scope can supersede. For this reason it's a best practice to explicitly state the scope. Also, I hear it's faster to state the scope because the CF runtime compiler does not have to walk through the scopes to find a match -- but this may be old info.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 09, 2016 1
LEGEND ,
Nov 14, 2016

Copy link to clipboard

Copied

Also, I hear it's faster to state the scope because the CF runtime  compiler does not have to walk through the scopes to find a match -- but  this may be old info.

When referencing an unscoped variable "id", CF walks through scopes in the following order, looking for "id":

LOCAL (VAR)

LOCAL (THREAD)

QUERY

FUNCTION

CGI

FILE

URL

FORM

COOKIES

CLIENT

It's always, always, always a good idea to scope reference your variables.  If for no reason other than keeping things straight, and the efficiency of CF server not looking through all the scopes searching for a variable; then at least for security sake.

If you're expecting a form.id but don't scope it, and someone slips in a URL.id, the URL "id" will be considered before the form "id".  Bad mojo.

HTH,

^_^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 14, 2016 0
New Here ,
Dec 12, 2016

Copy link to clipboard

Copied

I know it's been a while since you asked this question, but I thought this appropriate. I wrote this code for my team a while back so they could understand the difference in scopes and WHY vars had to be scoped. Without dumping/attaching the 3 pages of code here (because i don't see any way to do that), i'll just explain it.

I would also suggest you and any others give this a try also.

Index.cfm:

   sets some variables, all named "ID" in different scopes (application, session, client, form, variables, and URL)

   then asks the user which will he see on the next page if CF is just asked to return unscoped "#ID#".

<cfset client.id = 305> <!--- DONNA --->

<cfset session.id = 319> <!--- James --->

<cfset variables.id = 318> <!--- Adam --->

etc. 

and the form is posted via GET so that another ID is created in URL.

The 2nd page actually uses the unscoped ID to query a real DB for the name, and displays the answer. Then I delete the variables one by one and bounce back to index so my team can see the order in which they are resolved.

In case you're curious, the order is URL,FORM,client, ... and then blows up (Variable ID undefined)

because the remaining vars have to be specifically scoped in order to retrieve them.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 12, 2016 0