Live data from Hacker News

PHP: md5('240610708') == md5('QNKCDZO')

3v4l.org

21–30 of 185 posts

Re: PHP: md5('240610708') == md5('QNKCDZO')

#21

PHP's type coercion is nothing like I have every seen in any other language. Its horrendously messy, ugly and completely inexcusable. Strings type-casted to integers are 0. Seriously? Take a look at this, > $arr = array(0, "was", "invented", "in", "india"); > var_dump( in_array("Hello", $arr ) ); and yeah it is TRUE because "Hello" got coerced to 0. I blogged about a major bug, I faced, in PHP, where column name "10t…

> in_array(.., .., $strict)

I think you're aware of the third parameter but for anyone who reads this post, it disables the type coercion of the in_array call.

Re: PHP: md5('240610708') == md5('QNKCDZO')

#22
post #8
post #5

PHP's == has a lot of oddball effects. They were put in so that things would behave the way a novice expects them to (3 == '3') but would confuse more experienced programmers, or those coming from other languages. Unless you're deliberately taking advantage of automatic type conversion and whatnot, you should probably use === by default.

Designers of future languages, please take this example as a proof of the rule: don't design anything for newbies. They will find a way to make an error anyway, but dumbs-based design will be the problem for everyone else.

That - and the error is going to be a lot more subtle and harder to find.

In all fairness though, it's a balancing act - There are benefits to dynamic typing, but PHP clearly overdid it. (See also the disaster that was/is magic quotes)

Re: PHP: md5('240610708') == md5('QNKCDZO')

#23
post #22
post #8

Earlier quoted context omitted.

Designers of future languages, please take this example as a proof of the rule: don't design anything for newbies. They will find a way to make an error anyway, but dumbs-based design will be the problem for everyone else.

That - and the error is going to be a lot more subtle and harder to find. In all fairness though, it's a balancing act - There are benefits to dynamic typing, but PHP clearly overdid it. (See also the disaster that was/is magic quotes)

I believe you are confusing dynamic with weak typing. Other languages got dynamic typing quite right.

Re: PHP: md5('240610708') == md5('QNKCDZO')

#25
post #5

PHP's == has a lot of oddball effects. They were put in so that things would behave the way a novice expects them to (3 == '3') but would confuse more experienced programmers, or those coming from other languages. Unless you're deliberately taking advantage of automatic type conversion and whatnot, you should probably use === by default.

> you should probably use === by default.

unfortunately this can also backfire if your class/module is used in a different context where it gets strings instead of integers and you were just using === without really thinking about it:

We had a case where the code was something like:

  function doSomething($value) {
    if ($value === 0) {
      //do something
    } else {
      //do something else
    }
  }
This was then used in a slighly different context where $value was a string '0', it then ended up incorrectly in the //do something else block, doing the completely wrong thing. In this case the type co-erced == would have been better, and I think what the developer was expecting would be a type error due to the === but it's not a type error, it'll just fall into the else block.
Post reply on HN