It seems strange to cast operands to boolean, since it is the least informative of all data types, and makes the == operator least discriminating.
This is built into PHP. The == operator is capable of being used between values of different types. When two values are of different types, it will typically cast the more complex one to the simpler one.
Isn't there an operator to compare without any casting? (I.e. which evaluates to false whenever the types of the operands is not equal?)
Yes, it is ===.
Here is a test script and its output:
set a true;
set b int 1;
set c 1;
set d TRU;
set e int 0;
set f 0;
print . "d has the integer value of " int var d;
sub compare x y:
if === var #x var #y:
echo #x and #y are identical;
elseif == var #x var #y:
echo #x and #y are equal but not identical;
else:
echo #x and #y are unequal;
endif;
endsub;
gosub compare a a;
gosub compare a b;
gosub compare a c;
gosub compare a d;
gosub compare b b;
gosub compare b c;
gosub compare b d;
gosub compare c c;
gosub compare c d;
gosub compare d d;
gosub compare d e;
gosub compare d f;
OUTPUT:
d has the integer value of 0
a and a are identical
a and b are equal but not identical
a and c are equal but not identical
a and d are equal but not identical
b and b are identical
b and c are equal but not identical
b and d are unequal
c and c are identical
c and d are unequal
d and d are identical
d and e are equal but not identical
d and f are unequal
Here's what's going on. a is a Boolean, b and e are both integers, and the rest are strings. When b, c, or d is compared with a, the == operator casts it to its Boolean value, which is true. When a string is compared with a number, the == operator casts the string to its numeric value for the comparison. If the string does not match a decimal number, its numeric value will be 0. This is equal to e, which is numeric. When two strings are compared, there is no casting, and they are compared as strings. This is why d and f are unequal.
The == operator is useful when you think you might be comparing numeric strings with actual numbers. The === operator is useful when it is important for the values being compared to be the same type. It is thanks to using the == operator that a bug was found in your code. If you had used the === operator, that bug would have remained hidden longer. Remember that the bug had to do with using a string literal as though it were a constant, not with using == instead of ===.
This is built into PHP. The == operator is capable of being used between values of different types. When two values are of different types, it will typically cast the more complex one to the simpler one.
Yes, it is ===.
Here is a test script and its output:
Here's what's going on. a is a Boolean, b and e are both integers, and the rest are strings. When b, c, or d is compared with a, the == operator casts it to its Boolean value, which is true. When a string is compared with a number, the == operator casts the string to its numeric value for the comparison. If the string does not match a decimal number, its numeric value will be 0. This is equal to e, which is numeric. When two strings are compared, there is no casting, and they are compared as strings. This is why d and f are unequal.
The == operator is useful when you think you might be comparing numeric strings with actual numbers. The === operator is useful when it is important for the values being compared to be the same type. It is thanks to using the == operator that a bug was found in your code. If you had used the === operator, that bug would have remained hidden longer. Remember that the bug had to do with using a string literal as though it were a constant, not with using == instead of ===.