Skip to main content
Inspiring
March 11, 2015
Answered

IsDefined doesn't like variable names beginning with a number. Unless they are defined...

  • March 11, 2015
  • 1 reply
  • 3974 views

Hello,

I have detected a weird (to me) behaviour of the IsDefined function. I have something like:

<cfif IsDefined("url.vose")><cfset somevariable="some value"></cfif>

<cfif IsDefined("url.3d")><cfset somevariable="some value"></cfif>

* if url.3d is defined, good. All works as expected.

* if url.3d is not defined, CF throws a "Parameter 1 of function IsDefined, which is now url.3d, must be a syntactically valid variable name" error.

* if I use structKeyExists(url,"3d") instead of IsDefined("url,3d"), works as expected.

I tried with different combinations of variable names and scopes (pe, url.5d or form.3d) and the problem seems to be the number at the beginning of the variable name.

Is this the expected behaviour? I found very strange that it works if the variable exists and only throws an error if not. Could this be considered a bug and should be reported?

TIA,

This topic has been closed for replies.
Correct answer WolfShade

It doesn't matter if the language is CF or JavaScript or PHP or HTML - variable names are considered invalid if they do not begin with a letter or underscore.  You should _not_ prefix a variable name with a number.

Also, I've heard many developers complain about IsDefined().  Use StructKeyExists(), instead.

<cfif StructKeyExists(url,'vose')> blah blah blah </cfif>

<cfif NOT StructKeyExists(url,'vose')> blah blah blah </cfif>

V/r,

^_^

1 reply

WolfShade
WolfShadeCorrect answer
Legend
March 11, 2015

It doesn't matter if the language is CF or JavaScript or PHP or HTML - variable names are considered invalid if they do not begin with a letter or underscore.  You should _not_ prefix a variable name with a number.

Also, I've heard many developers complain about IsDefined().  Use StructKeyExists(), instead.

<cfif StructKeyExists(url,'vose')> blah blah blah </cfif>

<cfif NOT StructKeyExists(url,'vose')> blah blah blah </cfif>

V/r,

^_^

Inspiring
March 11, 2015

Thanks for your answer. That was my first thought, but then, the error should also appear when the variable exists....   ???

WolfShade
Legend
March 11, 2015

CF (and other languages) may or may not "see" a variable that begins with an invalid character, paramed or initialised.  But it's still invalid and prone to (as you have discovered) errors.

Best practice is to always declare or set a variable that is named beginning with a letter (upper- or lower-case) or underscore (AFAIK, the underscore is the only valid special character that can be used in the beginning of a variable name.)

What I typically do is prefix a variable name based upon what it's for, followed by an underscore, then what I would like to actually call the variable.

For example, if I need variables placed in the session scope for a form (which is handy for server-side validation, as you can submit to an action page, then CFLOCATE back to the form and have the values pre-populate the form upon page load), then I will:

<cfset session.frm_firstName = form.firstName /> on the action page.  At the very TOP of the page that contains the form, I will <cfparam name="session.frm_firstName" default="" />.  So if the user enters "Aolishus" as the first name, and the form doesn't validate, then when the app redirects back to the form, the first name field will contain what the user entered in the form.

Just my $0.03482

V/r,

^_^