views:

139

answers:

4

I've used C++ and Java before and they don't have this === operator.

How come they manage without it but in languages like PHP its key.

+3  A: 

Because PHP is not type safe. == compares 2 values, but === compares the values AND checks if their class types are the same.

I believe "2" == 2 returns true, while "2" === 2 returns false.

Joachim VR
+9  A: 

Actually equals in Java and == in C# act like === does in php. I.e. "24".equals(24) will return false.

What java and C# don't have an equivalent of is PHP's == (i.e. an operator/method such that "24".fuzzyEquals( 24 ) would return true). And that's because C# and Java are strongly typed and such an operator would be against their philosophy.

sepp2k
A: 

Just so you know, this is the same in JavaScript and according to JSLint should ALWAYS be used as a type-check, which technically makes == redundant. But I guess that's just personal preference

Alex
+2  A: 

when we say "A is equal to B" this can mean several quite different things

  • A and B are the same thing
  • A and B have the same value, that is, their values are not distinguishable for a third party
  • A and B can be converted to strings (or numbers) that are equal
  • A and B have the same hash value

etc

most languages do have different operators or functions for different kinds of equality , see http://en.wikipedia.org/wiki/Equality_%28relational_operator%29#Object_identity_vs._Content_equality

stereofrog