• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Mar 11, 2015 Mar 11, 2015

Copy link to clipboard

Copied

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,

Views

3.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 11, 2015 Mar 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,

^_^

Votes

Translate

Translate
LEGEND ,
Mar 11, 2015 Mar 11, 2015

Copy link to clipboard

Copied

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 11, 2015 Mar 11, 2015

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 11, 2015 Mar 11, 2015

Copy link to clipboard

Copied

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 01, 2019 Nov 01, 2019

Copy link to clipboard

Copied

I am getting this error as well under differenct circumstances and was hoping to get some guidance.  

Parameter 1 of function IsDefined, which is now Form.4316664, must be a syntactically valid variable name. 

This is the exact error that I am getting and the code snippet below is the code that I beleive is throwing the error.

 

<cfloop list="#Form.INVOICEITEMS#" index="i">
	<cfoutput>
		<cfif isDefined('Form.#i#')>			
			<cfquery name="getGameStatus" datasource="Binkley">
				Select gamingSplit From tdL_LTYPES Where LIC_CODE='#ListGetAt(form[i],1,'|')#'
			</cfquery>

 

 I've only had ownership of the program since June and this is something that the person I took ever for didn't cover with me.  This has been working until recently; so any guidance would be greatly appreciated.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 01, 2019 Nov 01, 2019

Copy link to clipboard

Copied

Hello, srouse,

 

I've been doing some researching, and cannot find the "rules" for ID and name attributes.  But last I heard, names and IDs should start with a letter or underscore.  So, if your form has elements that are named using strictly numbers, it shouldn't work.  However, using your code if IsDefined() is triggering an error, then StructKeyExists(form,i) should work, better.  I've heard many accounts over the last decade about how IsDefined() doesn't always work as intended.

 

Also, I noticed that in your CFLOOP, you are not declaring | as a delimiter, but in your query you are.  Default is comma.

 

HTH,

 

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 01, 2019 Nov 01, 2019

Copy link to clipboard

Copied

Thank you so much for your help - I changed IsDefined() to StructKeyExists and that cleared the errpr that I was getting.

 

Thank you very much

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 01, 2019 Nov 01, 2019

Copy link to clipboard

Copied

LATEST

You're welcome.  Glad I could be of some help.

 

V/r,

 

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation