CFLib.org – Common Function Library Project

getVariableScope(localVar)

Last updated August 06, 2004

author

Mosh Teitelbaum

Version: 1 | Requires: CF5 | Library: UtilityLib

Description:
Function that determines which scope an unscoped variable refers to. Only checks those scopes through which ColdFusion searches. Does not check named scopes (i.e., query names, etc.).

Return Values:
Returns a string.

Example:

<CFSET foo = "bar">
<CFSET URL.foo = "bar">
<CFSET COOKIE.foo = "bar">
<CFSET FORM.foo = "bar">

<CFOUTPUT>
#getVariableScope("foo")#
</CFOUTPUT>

Parameters:

Name Description Required
localVar Variable name to check. Yes

Full UDF Source:

/**
 * Function that determines which scope an unscoped variable refers to.
 * 
 * @param localVar      Variable name to check. (Required)
 * @return Returns a string. 
 * @author Mosh Teitelbaum (mosh.teitelbaum@evoch.com) 
 * @version 1, August 6, 2004 
 */
function getVariableScope(locVar) {
    var scopeList = "VARIABLES,CGI,FILE,URL,FORM,COOKIE,CLIENT,APPLICATION,SESSION,SERVER,REQUEST,CFHTTP,CALLER,ATTRIBUTES,ERROR,CFCATCH,CFFTP";
    var listEle = "";
    var cnt = 1;

    while (cnt LTE ListLen(scopeList)) {
        // Get current list element
        listEle = ListGetAt(scopeList, cnt);

        // Check for existence within current scope.  CGI is a special case
        if (listEle is "CGI" AND structKeyExists(cgi, locVar)) {
                return listEle;
        } else if (not listEle is "CGI" AND IsDefined("#listEle#.#locVar#")) {
                return listEle;
        }

        // Increment counter
        cnt = cnt + 1;
    }

}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson