structKeyExists() vs IsDefined()
On a self submitting form (like a Contact Us)...
Why is <cfif structKeyExists(form, "submitform")> preferred over <cfif IsDefined('FORM.submitform')>?
What is the advantage/benefit of structKeyExists?
Thanks
On a self submitting form (like a Contact Us)...
Why is <cfif structKeyExists(form, "submitform")> preferred over <cfif IsDefined('FORM.submitform')>?
What is the advantage/benefit of structKeyExists?
Thanks
isDefined() will look through a bunch of scopes looking for a variable fo that name to exist, whereas structKeyExists() just does what it's told.
As a slightly contrived example, if you defined a variable variables.form.submitform, then isDefined("form.submitform") would return true. structKeyExists() would return false.
It's less of a consideration if you scope your variables all the time, but isDefined() can still be defeated.
Using structKeyExists() also makes more sense if you're looking at a struct and wanting to know if it's got a key of a given name (which is what you're wanting to do here).
Another consideration regarding isDefined() and structKeyExists() is that I have had mixed results getting isDefined() to work with session variables. I've seen it say "no" when it means "yes", and vice-versa.
isDefined() is an imprecise function and I do not trust it, so I do not use it.
--
Adam
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.