• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Difference in scope?

Explorer ,
Nov 07, 2016 Nov 07, 2016

Copy link to clipboard

Copied

Hi All,

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

Thanks!

John

Views

314

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
community guidelines
Community Expert ,
Nov 07, 2016 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">

Votes

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
community guidelines
Advocate ,
Nov 09, 2016 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.

Votes

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
community guidelines
LEGEND ,
Nov 14, 2016 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,

^_^

Votes

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
community guidelines
New Here ,
Dec 12, 2016 Dec 12, 2016

Copy link to clipboard

Copied

LATEST

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.

Votes

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
community guidelines
Resources
Documentation