Jump to content

a basic comparison case


Recommended Posts

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

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' page
Note: 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 by mikell
Link to comment
Share on other sites

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 by AspirinJunkie
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...