View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > IsDefined PreviousNext

IsDefined

Evaluates a string value to determine whether the variable named in it exists.

This function is an alternative to the ParameterExists function, which is deprecated.

True, if the variable is found, or, for a structure, if the specified key is defined; False, otherwise.

Returns False for an array or structure element referenced using bracket notation. For example, IsDefined("myArray[3]") always returns False, even if the array element myArray[3] has a value.

Decision functions

IsDefined("variable_name")

Evaluate

ColdFusion MX: Changed behavior: this function can process only the following constructs:

Parameter

Description

variable_name

String, enclosed in quotation marks. Name of variable to test for.

When working with scopes that ColdFusion exposes as structures, the StructKeyExists function can sometimes replace this function. The following lines are equivalent:

if(isDefined("form.myVariable"))
if(structKeyExists(form,"myVariable"))

<cfif IsDefined("form.myString")>
   <p>The variable form.myString has been defined, so show its contents.
   This construction allows us to place a form and its resulting action code
   on the same page and use IsDefined to control the flow of execution.</p>
   <p>The value of "form.myString" is <b><i>
   <cfoutput>#form.myString#</cfoutput></i></b>
<cfelse>
   <p>During the first time through this template, the variable "form.myString" 
   has not yet been defined, so we do not try to evaluate it.
</cfif>

<form action="#CGI.Script_Name" method="POST">
<input type="Text" name="MyString" value="My sample value">
<input type="Submit" name="">
</form>

Contents > CFML Reference > ColdFusion Functions > IsDefined PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


Corey Gilmore said on Nov 18, 2003 at 8:11 AM :
IsDefined will always return true for CGI.* irrespective of the actual existence of the variable.

<CFIF IsDefined("CGI.UndefinedVar")>
You shouldn't see this - but you will...
</CFIF>
Corey Gilmore said on Nov 18, 2003 at 12:40 PM :
The behavior mentioned above is documented at
<a href="http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/expres33.htm">http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/expres33.htm</a>
Corey Gilmore said on Feb 6, 2004 at 3:22 PM :
It doesn't appear my last comment posted.

IsDefined does not work, nor does it appear there is any other way to check for the value of a member of a struct, if you're referencing it using a variable member name.

For example, if you are looping like this:
<cfloop LIST="#NewKeys#" DELIMITERS="," INDEX="val">
IsDefined( "stOrig.#val#" ) does not work
IsDefined( "stOrig.val" ) does not work
IsDefined( "stOrig.[val]" ) does not work
StructFindKey cannot be used
StructKeyExists only checks for the existence of the key, not the value.

The problem occurs when you try and perform any type of comparison between two values inside of the loop:
<CFIF stOrig[val] NEQ stUser[val]>
halL said on Feb 9, 2004 at 6:17 AM :
You can use either of the following ways to determine whether a dynamically named key exists:
Use the & string concatenation operator to add the key name in IsDefined, as in IsDefined("myStruct."&keyVar).
Use the StructKeyExists function with the variable name as the key parameter.
The following code shows both:

<cfset var1="foo">
<cfset var2="bar">
<cfset Mystruct.foo="Hello World">
<cfoutput>
#structkeyExists(mystruct, var1)#<br>
#structkeyExists(mystruct, var2)#<br>
#Isdefined("Mystruct."&var1)#<br>
#Isdefined("Mystruct."&var2)#
</cfoutput>
yflicker said on Jun 12, 2004 at 3:58 PM :
I'm trying to test whether an application-scoped variable is defined by using the following code:

(1) <cfif IsDefined("application.departmentName")>...

However, when I run this code, I get the following error: "The requested scope application has not been enabled", i.e. no, the variable is not defined. It's not clear to me why the function would throw an error versus returning false (which I would think would be the logical thing to do) if the application scope has not been enabled. Anyway, what is the proper way for testing the above condition?

(2) <cfif StructKeyExists(application, "departmentName")>...?
Is this the correct way? If anything, I would think it would be more logical if this code in (2) threw an error (as opposed to the code in (1)).

(3) <cfif IsDefined("application") and StructKeyExists(application, "departmentName")>...?
Is this the correct way? (1) makes the most sense to me...

Thanks
mydognacho said on Aug 3, 2004 at 11:26 AM :
I'm playing with IsDefined and not getting the results that the CF5 language ref says I would get. IE:

<cfset andy = "matthews">
<cfoutput>
IsDefined quotes: #IsDefined(andy)#<br>
</cfoutput>

The book says that without quotes around a var, IsDefined checks to see if the contents of a variable exist rather than the variable itself. You can see that the var has contents, but when I run the code above, I get "NO", insted of YES. Any thoughts?
Glowball said on Aug 24, 2004 at 9:04 AM :
Is there no way to "undefine" or unset a simple variable once it has been defined?
jrunrandy said on Aug 25, 2004 at 9:44 AM :
The only thing I can think of is to put the variable in a structure and use the StructDelete function to delete it.
ChronoCrypt said on Sep 9, 2004 at 12:15 PM :
Following code throws an error after it outputs the variable...?

Error: Parameter 1 of function IsDefined, which is now "tempStructure.tempArray[1]", must be a syntactically valid variable name.

<cfset tempStructure = structNew()>
<cfset structInsert( tempStructure, "tempArray", arrayNew( 1 ) )>
<cfset arrayPrepend( tempStructure.tempArray, "tempValue" )>
<cfoutput>#tempStructure.tempArray[1]#</cfoutput>
<cfif isDefined( "tempStructure.tempArray[1]" )>
tempStructure.tempArray[1] exists
</cfif>
infamouskirch said on Nov 22, 2004 at 4:00 PM :
the following code:

< cfif isDefined("Session.MM_Username")>
< a href "logout.cfm">Click here to log out</a>
<cfoutput>#Session.MM_Username#</cfoutput>
<cfelse>
&nbsp;
</cfif>

will return the logout link and a blank as the Session.MM_Username regardless to whether or not the Session.MM_Username is defined...

Otherwise

< cfif isDefined(Session.MM_Username)>
< a href "logout.cfm">Click here to log out</a>
<cfoutput>#Session.MM_Username#</cfoutput>
<cfelse>
&nbsp;
</cfif>

returns the error "Session.MM_Username is NOT defined" which is what I'm testing for, right ?

Conditional statements shouldn't be this confusing
cannedRadio said on Jan 18, 2005 at 11:07 AM :
IsDefined("myArray[3]") does not return "false" it produces an error. Tested in CF 5, mx, mx 6.1. You can not check for the existence of an array element, by copying the element to a simple variable and use IsDefined to test whether the simple variable exists, because you get a error trying to use the array to set the simple variable.
here is a helpful udf with example you may use until CF can handle this requirement.

<cfscript>
function isArrayElementDefined(arrayName){
try{
testArrayVal = evaluate(arrayName);
return true;
}catch(any testForArrayElement){
return false;
}
}
</cfscript>
<cfset myArray = arrayNew(1)>
<cfset myArray[1] = "bob">
<cfset myArray[3] = "joe">
<cfoutput>
<cfloop from = "1" to = "3" index = "place">
#isArrayElementDefined("myArray[#place#]")#
</cfloop>
</cfoutput>

output: true false true
mocax said on Feb 21, 2005 at 6:31 PM :
to unset a variable, use structdelete on the "variables" structure.

eg.
<cfscript>
thisone = "ok";
writeoutput("<div>#isdefined('thisone')#</div>");
structdelete(variables,"thisone");
writeoutput("<div>#isdefined('thisone')#</div>");
</cfscript>
dorange said on Aug 23, 2005 at 2:25 PM :
Here is a scoping problem that was driving me crazy until i figured out what's going on. I've defined a function that had url as one of it's arguments names
<cfargument name="url" required="true" type="string">
within the function i was testing for existiance of a url parameter as usual
<cfif isDefined("url.foo")>
However, this always returns FALSE since CF6.1 is looking at the function arguments scope first and since arguments.url is a string it returns always false no matter what the url query string is. Whether this is a feature or a bug is debatable but is sure confusing. So watch out.
No screen name said on Sep 21, 2005 at 11:47 AM :
I just ran into a problem using isDefined(). i just upgraded to 6.1 last week for another bug that was causing me problems. Now this is coming up. But not everythere.

<form action="" method="post">
<input name="manual_accept" type="image" src="button_accept.gif" border="0">
<input name="manual_reject" type="image" src="button_reject.gif" border="0">
</form>

<cfif isDefined("form.manual_reject.x")>
works when manual reject is pressed
</cfif>
<cfif isDefined("manual_reject.x")>
DOES NOT work with manual reject is pressed
</cfif>

<cfif isDefined("manual_accept.x")>
works when manual accept is pressed
</cfif>

I have isDefined() all through out my application without for form scope. Is this a new bug for Cold Fusion?
guessed said on Sep 23, 2005 at 1:31 AM :
@ no screen name [sep 21]:
For your information; I get the following output when I click 'reject':

works when manual reject is pressed
DOES NOT work with manual reject is pressed

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funct130.htm