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.

Returns

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

The return value is 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

Function syntax

IsDefined("variable_name")

See also

Evaluate

History

ColdFusion MX: Changed behavior: 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 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"))

Example

<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>

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

Version 7

Comments


twillerror said on Feb 22, 2005 at 10:09 AM :
IsDefined does not work for an array. It is possible to set certain positions in an array and not others. Like spot 1,2,4 but not 3.

IsDefined ( "array[3]" ) does not work.

Currently there is a function on cflib.org that pulls this off by using a cftry cfcatch statement to catch coldfusion.runtime.undefinedelement or something like that.

I don't see an array function that can determine if an array element has been defined. It is rare that we use an array in such a manner, but it is useful sometimes.
dfSDL JFSD:LIcfS said on Mar 2, 2005 at 9:57 AM :
The IsArray function will do just what your looking for.
IsArray("Myarray[2]") will retrun true if this array and index are valid. See the IsArray reference in this guide
spdygnlz said on May 3, 2005 at 11:05 AM :
Actually the IsArray() doesn't work either. I recommend using the cfparam tag. See the following article:

http://coldfusion.sys-con.com/read/41900.htm

 

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

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000520.htm