Skip to main content
Inspiring
November 7, 2016
Question

Difference in scope?

  • November 7, 2016
  • 3 replies
  • 423 views

Hi All,

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

Thanks!

John

    This topic has been closed for replies.

    3 replies

    Participant
    December 12, 2016

    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.

    Legend
    November 10, 2016

    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.

    WolfShade
    Legend
    November 14, 2016

    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,

    ^_^

    BKBK
    Community Expert
    Community Expert
    November 7, 2016

    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">