3 ms·
Who ever thought of this? I can understand "don't use == for strings", or implicit conversion when one of the arguments is a number, but this is extremely sneak
by wladimir 15y ago
Who ever thought of this? I can understand "don't use == for strings", or implicit conversion when one of the arguments is a number, but this is extremely sneaky as it will only behave this way with two numerically-looking strings. Ouch. Why does it even do that check, wasting cycles beyond a normal string comparison? It looks like an elaborate and cruel trap for novice programmers.
Edit: I know == is not a string comparison. But you'd expect it to fail in a predictable way when passed strings that are not parseable as numbers, instead of trying to fall back on a string comparison so that people get the wrong idea.
- dchest 15y agoNot defending the bad design decision, but string comparison in PHP is strcmp, not ==.
- pornel 15y agoPHP gets input from various places, and one might want "1.00" from a form or URL to equal "1.0" read from a cookie or via database adapter that stringifies everything.
- kingkilr 15y agoIf only we had some sort of way of explicitly telling our computers that we wanted to to convert a sequence of characters into a number.
- mkup 15y agoThis thing already exists, it is called "strong typing" (in C++, Go, Java etc).
- Ralith 15y agoYes, that was the joke.
- varelse 15y agoWhat I don't understand is why all these weak-typed languages don't optionally allow one to strong type a variable.
- maratd 15y agoThat's like saying why doesn't someone create a combination hammer and screwdriver. A proper tool for every job.
- varelse 15y agoAnd as much as I love python and javascript, I'd really like to use a screwdriver now and then rather than leave hammer marks everywhere, especially if they are being used for something beyond formatting HTML. This hamdriver you speak of: tell me more.
- justincormack 15y agoWeak typing does not require bonkers coercion of types.