Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8058377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:20:12+00:00 2026-06-05T09:20:12+00:00

Having not worked with coldfusion before I was asked to look into some strange

  • 0

Having not worked with coldfusion before I was asked to look into some strange intermittent bugs in a coldfusion application.

After reading about scopes I believe the problem is because none of the variables in my cfc functions are using the var keyword, and the same variable name is used in various functions. So as I understand it the variables are scoped at page level and different threads calling these functions will be overwriting the variable causing the “strange” problems.

My question is what is the proper way to do this?

 <cfset var listCount = 0>
 <cfquery name="qGetElementsByType" dbtype="query" maxrows="#arguments.num_to_return#">
    SELECT elementId,
           title, PIhtml, Rerhtml,
           text, url, image, Rank, isPoll, pollId, subjectId
    FROM   arguments.element_query
    WHERE  <cfloop list="#arguments.element_type_id#" index="lcv">
               <cfif listCount GT 0>
                  OR
               </cfif>
               subjectid =  #lcv#
              <cfset listCount = listCount + 1>
           </cfloop>
</cfquery>

Does var need to be added each time the listCount variable is set, or just on the initial declaration?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:20:14+00:00Added an answer on June 5, 2026 at 9:20 am

    (I hope this answer isn’t too long-winded. I didn’t think the existing answers gave sufficient information, but hopefully haven’t gone too far the other way…)

    In CF, there are assorted scopes which variables can be placed in (application,session,url,cgi,etc).

    Some of these require explicit declaration to be used (e.g. session variables must always be scoped), others can be automatically accessed when reading a variable (e.g. form and url variables can be read by using an unscoped variable) – there is an order of precedence here which determines which scopes are checked for an unscoped variable.

    The bottom scope in this ordering is the variables scope, which is a scope which applies for the entire current page/object instance.

    When setting a new variable, if unscoped, it is created in the variables scope. Since this is a global scope, it can be accessed from both different instances of the same function, as well as difference functions, which causes the issues of which you’re aware.

    To prevent a variable going into the global variables scope you must place it in the function’s local scope. (Technically you can place it in the function’s arguments scope, but this will likely confuse people.)

    In earlier versions of CF, there was no way to explicitly access the local scope – you needed to use the var keyword in order to create the variable inside the local scope – and once created it will always take precedence (both for reading and writing) over the variables scope.

    With CF9, the local scope is now a “proper” scope and can be accessed explicitly, so instead of using <cfset var x = 0 /> you can write <cfset local.x = 0 /> – the main benefit of this is when you are creating a variable where the var keyword cannot be used, e.g. <cfquery name="local.qGetElementsByType" ...> and <cfloop index="local.lcv"...>

    You still only need to apply the local scope when first creating each variable, to prevent it going into the variables scope – subsequent reads/updates can be unscoped if you prefer, just as they would be when doing the var scope.
    (Although there are other potential scope-related issues with unscoped variables, such as inside of a <cfloop query="queryname"> block, and because of this some people will argue you should always scope all variables no matter what.)

    In summary, to make the code you show safe, you need to scope:

    • qGetElementsByType from the cfquery tag
    • lvc from the cfloop tag

    Since these variables are not created with cfset, scoping is most easily done by prefixing the names with local.

    Since you have already var scoped the listCount variable, you don’t need to do it again in the same function – you can optionally use <cfset local.listCount = local.listCount + 1> (or indeed <cfset local.listCount++ > ) but again this is a matter of preference and not needed to protect against leaking into the variables scope.

    (side note: Ideally, you should use the cfqueryparam tag around #lcv# to protect against SQL injection – even though this is a query of query this might still be an issue, and it’s always better to play it safe on security.)

    Of course, that’s just this function – you’ll also need to fix other functions – and an easy way to do that is to use the varscoper tool to scan your entire codebase and identify variables that need scoping.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not having worked with Objective C before I thought I would give a stab
I've just started working with C++ after not having worked with it for quite
I've run into problems on numerous occasions of users not having one of the
I'm retriving contacts from WP7. Some contacts having images some not having. I want
Having not worked with cartesian graphs since high school, I have actually found a
I am having to implement a payment gateway in Rails that I've not worked
Having recently worked on a project which required some more IO interaction than I'm
See Having Some Issues With Making Pacman to understand this question fully. Just look
I'm not having much success when attempting building pgmagick on OS X Lion with
I'm not having much luck centering vertically the label I'm adding to the TitleView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.