I’m trying to determine if a variable with a variable name is defined. Please help with my syntax… my attempts so far:
<cfif isDefined(Evaluate("session['#url.sessionSQL#']['SQL_ALL']"))>
<cfif isDefined('Evaluate("session[#url.sessionSQL#]")["SQL_ALL"]')>
<cfif isDefined(Evaluate("session['#url.sessionSQL#']['SQL_ALL']"))>
<cfif isDefined('session[Evaluate("#url.sessionSQL#")]["SQL_ALL"]')>
<cfif isDefined('session["#url.sessionSQL#"]["SQL_ALL"]')>
Thanks.
Your question is a little confusing 😉
If session[url.sessionSQL][‘SQL_ALL’] contains the name of a variable, you can use structKeyExists to verify that variable exists in a particular scope.
On the other hand, if just want to verify those session variables exist
Either way, you do not need the evaluate() function.
Update: From comments, a key difference between
IsDefinedandStructKeyExistsis precision. IsDefined examines a whole list of scopes when deteriming if a variable exists. Usually (though not always) that is undesirable because it can lead to unexpected results if you forget a particular variable exists in multiple scopes. (UsingIsDefinedinside a function is a prime example.) When you specifically want to check multiple scopes, thenIsDefined()is more appropriate. Otherwise, I would stick withStructKeyExistsas its results are less ambiguous.