satanico64 Posted August 24, 2017 Share Posted August 24, 2017 Hi guys, how are you today ? sea sex sun and holidays ? I want to understand an obvious comparison fact: I use to have many httprequests, for wich result was 'OK' or 'KO' Now for some of these requests, the result send is 1 or 0 I got a function for doing my request, so to test the answer, instinctively i did something like ; $myvar = my_http_request() ; sometimes return 0/1 sometimes returns 'OK/KO' $myvar = 0 ; For test If $myvar = "OK" Or $myvar = 1 Then ConsoleWrite("It's equal" & @CRLF ) ; > This is equal. Else ConsoleWrite("It's NOT equal" & @CRLF ) EndIf In this case, the comparison is always true. Ok i understand that it compare string to integer... but why is this always true ? .. i would have understood if it was always false but why always true ? that's all for me thanks guys, kids, ladies and gentlemen ! Link to comment Share on other sites More sharing options...
mikell Posted August 24, 2017 Share Posted August 24, 2017 (edited) Though it's not recommended, if you want to compare the same variable against a string or a number, you must be explicit If $myvar == "OK" Or $myvar = 1 Then Edit Details from the helpfile, 'Operators' pageNote: Care is needed if comparing mixed datatypes, as unless the == case-sensitive string operator is used, mixed comparisons are usually made numerically. Most strings will be evaluated as 0 and so the result may well not be the one expected. It is recommended to force the items being compared into the same datatype using Number/String before the comparison. Edited August 24, 2017 by mikell Link to comment Share on other sites More sharing options...
AspirinJunkie Posted August 24, 2017 Share Posted August 24, 2017 (edited) Comparison of different data types need a conversation of at least one operand to the datatype of the other. In this case the right operand "OK" of the =-operator gets converted to a integer (the datatype of the left operand) before it's compared. And because Int("OK" ) leads to 0 implicitly the comparison looks like: If $myvar = 0 [...] Edited August 24, 2017 by AspirinJunkie Link to comment Share on other sites More sharing options...
satanico64 Posted August 24, 2017 Author Share Posted August 24, 2017 (edited) i did. but i just don't think that the result of my first test is logical. (to me) Edit: Thanks @AspirinJunkie i understand Mystery solved ! Edited August 24, 2017 by satanico64 Link to comment Share on other sites More sharing options...
satanico64 Posted August 24, 2017 Author Share Posted August 24, 2017 obviously, thanks @mikell too. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now