View comments | RSS feed

IsDefined

Description

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.

Return value

True, if the variable is found, or, for a structure, if its 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.

Category

Decision functions

Syntax

IsDefined("variable_name")

See also

Evaluate

History

New in ColdFusion MX: this function can process only the following constructs:

Parameters

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

Usage

When working with the URL and Form scopes, which are structures, the StructKeyExists function can sometimes be used in place of this function. The following blocks of code are equivalent:

Example

<h3>IsDefined Example</h3>

<cfif IsDefined("FORM.myString")>
<p>Because the variable FORM.myString is defined, we can show its contents. 
This lets us put a FORM and its resulting action page in the same page,
while using IsDefined to control the flow of page execution.
<p>The value of "FORM.myString" is <B><I><cfoutput>#FORM.myString#
 </cfoutput></I></B>
<cfelse>
<p>During the first execution of this page, the variable "FORM.myString" is 
not yet defined, so it is not evaluated.
</cfif>
...

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

Version 6

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

Comments


bruceschuman said on Jun 12, 2002 at 6:59 PM :
Looking at this current documentation, I notice that there is no mention of a negative case for IsDefined.

But CF MX seems to insist (or at least strongly recommend)that IsDefined be used to replace ParameterExists.

There is no mention of a negative case in my CF 4.0 official CFML language reference (page 323), and the negative case is not mentioned in Ben Forta's book on 4.0, where IsDefined is discussed on p930. I looked for something generic about functions in general, where somethng about NOT or the negative might be mentioned in regard to functions, but couldn't find any reference to the option of negating IsDefined,

However, I was told last night on the Allaire developer/support forum that I can write the function as

<CFIF NOT IsDefined("variable")>

This seems to work -- and is probably essential to many working professional developers. It might be a good idea to mention this in the documentation.

Thanks for the good work.
bruceschuman said on Jun 12, 2002 at 7:02 PM :
Hmm. Am I reading you right? -- where it says under History that "IsDefined is new to CF MX" -- ??

That isn't true -- or am I missing some fine point?
ctina said on Jul 19, 2002 at 12:51 PM :
Update from Macromedia, in response to bruceschuman: the isDefined function can take the negative case, as in his example: <CFIF NOT IsDefined("variable")>.

The History section lists the properties of the isDefined function that have changed. The function itself is not new.

Christina Lamkin
CF documentation team
hallow said on Jan 8, 2003 at 12:44 PM :
In MX, IsDefined('session.blah'), where the session scope is not enabled, throws an exception. This differs from the behavior of 5.

In order to work around this, we surrounded the cfif using the IsDefined() with a cftry/cfcatch block, with no code in the cfcatch.

We use this in a common custom tag called by some applications that may or may not have the session scope available, which is why we check for IsDefined().

Corey Gilmore said on Feb 6, 2004 at 3:04 PM :
There is no way to check for the value of a member of a struct, if the name of the member you are checking for is another variable.

Inside of a loop 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
StructKeyExists(stOrig, "#val#") only returns if the key has been created, not if there is a null/undefined value.

It does not seem possible to use StructFindKey in any useful way either.
halL said on Feb 9, 2004 at 6:24 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 (tested on ColdFusion MX 6.1, but should work on ColdFusion MX) 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>
gravyface said on Jan 27, 2005 at 10:34 AM :
Apparently you can't test for local variables with IsDefined (in CF5 anyways).
I had to use CFPARAM. Seems kind of ghetto to me.
slarbi said on Mar 7, 2006 at 11:53 AM :
To the comment above about CF5 and not able to test local variables' existance using isdefined:

I never had a problem with it. It's been quite a while since using CF5, but I remember a problem I always used to have was forgetting to put the variable name in quotes. Otherwise it's testing if the string inside your variable exists as a variable - clearly not what you intended.

 

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

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt1130.htm