Skip to main content
John_Allred
Inspiring
Aug 6, 2021 4:39 PM UTC
Answered

Why does CFIF test give opposite results?

I ran into an issue where a condition (CFIF) that should be true is returning false. I'm hoping someone has a nifty answer to this.

 

Here's some code I created to demonstrate the test:

<cfif url.userid is "session.userid"> <!--- Condition True --->
    My test is &lt;cfif url.userid is "session.userid"&gt;<br><br> 
    url.userid IS equal to session.userid<br><br>  
    The values are:<br>
    url.userid = <cfoutput>#url.userid#</cfoutput><br>
    session.userid = <cfoutput>#session.userid#</cfoutput><br><br>
<cfelse> <!--- Condition False --->
    My test is &lt;cfif url.userid is "session.userid"&gt;<br><br> 
    url.userid is NOT equal to session.userid<br><br>
    The values are:<br>
    url.userid = <cfoutput>#url.userid#</cfoutput><br>
    session.userid = <cfoutput>#session.userid#</cfoutput><br><br>
</cfif>

And here's how my "Condition False" code displays:

I'm stumped.

 

Thanks!
John Allred

    This topic is closed to new replies. Start a new post to keep the conversation going.
    John_Allred
    John_AllredCorrect answer
    Inspiring
    Aug 6, 2021 4:57 PM UTC

    This is where you see my sheepish expression as I solve my own problem. For what it's worth, I'd love one of the gurus to explain "WHY", but here's the solution:

     

    I was using "IS" between the two variables. When I changed IS to EQ and removed the quotes, all is well. 

     

    So, this works as expected. With the variables holding the same value:

     

     

    <cfif url.userid eq session.userid>

     

    Could it be that I was comparing a numeric value with a text value?

     

    3 replies

    John_Allred
    John_AllredAuthorCorrect answer
    Inspiring
    Aug 6, 2021 4:57 PM UTC

    This is where you see my sheepish expression as I solve my own problem. For what it's worth, I'd love one of the gurus to explain "WHY", but here's the solution:

     

    I was using "IS" between the two variables. When I changed IS to EQ and removed the quotes, all is well. 

     

    So, this works as expected. With the variables holding the same value:

     

     

    <cfif url.userid eq session.userid>

     

    Could it be that I was comparing a numeric value with a text value?

     

    Translate
    EddieLotter
    Inspiring
    Aug 6, 2021 6:23 PM UTC

    Your original post shows that you're incorrectly comparing url.userid to the literal string "session.userid" rather than the value of session.userid, which you are correctly doing in your second post.

    Remove the quotes from the example in your first post and it will behave the way you expect.

    Translate
    BKBK
    Community Expert
    Community Expert
    Aug 7, 2021 11:28 AM UTC

    I agree with @EddieLotter . Another variation, likely the one you intended,  is:

    <cfif url.userid is "#session.userid#"> <!--- Condition True --->

     

    Translate