CFLib.org – Common Function Library Project

getVariableScope(localVar)

Last updated August 6, 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;
	}

}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson