News for hackers. Stuff that matters.
Re:PHP WTF: A PHP Annoyance Posted Every Day by catch23 on Friday June 19, 2009 @04:55PM (#665804)
in javascript:

1 == '01' --> true

0 == 'x' --> false

1 == '1' --> true

true == 'asdf' --> false

'-2' == 2 --> true

'-2' == 0 --> false

Seems like in javascript, the coercing is much more sane. In php, all of the above statements would end up true.

Re:PHP WTF: A PHP Annoyance Posted Every Day by ars on Friday June 19, 2009 @05:34PM (#665841)
Javascript is not totally sane:

  '\t\r\n' == 0 --> true
And not transitive:

  "false" == "0" --> false
  "0" == 0 --> true
  0 == false --> true
  false == "false" --> false
It's basically impossible to make a loosely typed equality perfect.

PHP is much more strict about the rules used when doing the comparison, javascript is more loose. It tries to guess.

When comparing a string to a number, if the string looks like a number it's converted to one. But if it doesn't, then instead the number get converted to a string.

This works well most of the time - but not always, since it can surprise you. PHP is more strict, so it doesn't work as often, but it fails more consistently, so it's easier to find the problem.

Another example:

  "1" == "01" --> false
  "01" == 1 --> true
  1 == "1" --> true
Re:PHP WTF: A PHP Annoyance Posted Every Day by BerislavLopac on Saturday June 20, 2009 @01:40AM (#666176)
The funny thing is that in Javascript:

12 == parseInt('012')

returns false. :)

Re:PHP WTF: A PHP Annoyance Posted Every Day by mhansen on Saturday June 20, 2009 @03:38AM (#666237)
A leading zero usually means 'this number is octal', so parseInt returns 10. Not very intuitive notation, however.
Re:PHP WTF: A PHP Annoyance Posted Every Day by encoderer on Friday June 19, 2009 @05:19PM (#665829)
I don't have a PHP runtime in front of me, but IIRC the first one on your list and the last 2 will evaluate false in php