Search code examples
if-statementcoldfusion

logic issue with compound ORs in an IF statement giving unexpected results


This is written in CF, but I don't think that's the issue. Although, I maybe wrong, I've been staring so long, it has my head all twisted.

The general idea is if the data structure is undefined or if a particular member is missing from the DS or the member is "", forward to another page to fix it. I don't generally like to do negative logic, but in this case, approaching positively or directly would make the 3 line IF clause a page long if/then/else.

    <cfset session.returnURL = "/future/dashboard.cfm">
    <!---
    <cfset session.agentCode = "">
    --->
    <cfset session.agentCode = "123456789sd">

    <cfdump var="#(isStruct(session) neq true)#">
    <cfdump var='#(StructKeyExists(session, "agentcode") neq true)#'>
    <cfdump var='#(session.agentcode eq "")#'>
    <cfdump var=#((isStruct("session") neq true)or(StructKeyExists(session,"agentcode") neq true)
                or (session.agentcode eq ""))#>

    <cfif ((isStruct("session") neq true)  or (StructKeyExists(session, "agentcode") neq true) 
                or (session.agentcode eq ""))>
        <cfoutput>#session.agentCode#</cfoutput>
        <cfexit>
        <!--- <cflocation url="/future/security/login.cfm" addtoken="No"> --->
    </cfif>
    .
    .
    .

The first three dumps output as expected: "No No No" or "No No Yes" , depending on which cfset is exposed for session.agentCode. But when I combine them with ORs in the fourth and in the IF statement, it returns Yes, no matter the 3 inputs. Normally, that means there is a nesting/precedence issue, but I don't see it. Three Nos into ORs should return a No!

Thank you!


Solution

  • It looks like the issue is the quotes around "session" in isStruct in the 4th and 5th dumps (note that they aren't there in the first one); if you take those away, it should work.