3 ms·
In the absence of strict type checking it would be terrible, but == performs a loose comparison, and "foobar" cannot cast to any other number than 0. Perl will
by daneel_w 6y ago
In the absence of strict type checking it would be terrible, but == performs a loose comparison, and "foobar" cannot cast to any other number than 0. Perl will go about it the exact same way. Contrast this against strict comparison, "0" === 0, which will evaluate as false.
- hypertele-Xii 6y agoMaybe "foobar" should evaluate to NaN.
- _-___________-_ 6y agoDoes PHP have separate int and float types? NaN is a float value and I would expect 0 to be an int.
- ragnese 6y agoIt does
- xeeeeeeeeeeenu 6y ago>Perl will go about it the exact same way. No, Perl is completely different and IMO it's the only mainstream dynamically typed language that has sane comparison operators. Perl has two sets of operators, numeric (==, +, *, <, >, <=> etc.) and stringwise (eq, ., lt, gt, cmp etc.). You can think of them as explicit (but concise) casts. For example, Perl's $foo eq $bar is roughly equivalent to Python's str(foo) == str(bar). It is true that "foobar" == 0 returns true in Perl but, as I showed above, it does that for a completely different reason.