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?
Copy link to clipboard
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>
Thanks for your answer. That was my first thought, but then, the error should also appear when the variable exists.... ???
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
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.
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.
^ _ ^
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
You're welcome. Glad I could be of some help.
^ _ ^