PHP expresses two different strings to be the same

Possible Duplicate:
php == vs === operator
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?

Why does the following statement return true ?

"608E-4234" == "272E-3063"

I have also tried this with single quotes around the strings. The only way I can get it to evaulate to false is by using the === operator instead of ==

My guess is PHP is treating it as some sort of equation but it seems a bit of a strange one.

Can anybody elaborate?


"608E-4234" is the float number format, so they will cast into number when they compares.

608E-4234 and 272E-3063 will both be float(0) because they are too small.

For == in php,

If you compare a number with a string or the comparison involves numerical strings , then each string is converted to a number and the comparison performed numerically.

http://php.net/manual/en/language.operators.comparison.php

Attention:

What about the behavior in javascript which also has both == and === ?

The answer is the behavior is different from PHP. In javascript, if you compare two value with same type, == is just same as === , so type cast won't happen for compare with two same type values.

In javascript:

608E-4234 == 272E-3063 // true
608E-4234 == "272E-3063" // true
"608E-4234" == 272E-3063 // true
"608E-4234" == "272E-3063" // false (Note: this is different form PHP)

So in javascript, when you know the type of the result, you could use == instead of === to save one character.

For example, typeof operator always returns a string, so you could just use

typeof foo == 'string' instead of typeof foo === 'string' with no harm.


PHP uses IEEE 754 for floats, and your numbers are so small that they evalue to 0.

See: http://en.wikipedia.org/wiki/IEEE_floating_point

Name        Common name         Base    Digits  E min   E max   
binary32    Single precision        2   23+1    −126    +127        
binary64    Double precision        2   52+1    −1022   +1023       

I think that PHP reads this as a scientific syntax, which will be translated as:

608 x 10^-4234 == 272 x 10^-3063 

PHP interprets this as being 0 = 0 .

链接地址: http://www.djcxy.com/p/37766.html

上一篇: 在关系数据库中存储分层数据有哪些选项?

下一篇: PHP表示两个不同的字符串是相同的