为什么PHP将带有字母E的字符串转换为一个数字?
为什么以下语句返回 true
?
"608E-4234" == "272E-3063"
我还尝试了围绕字符串的单语引号。我唯一可以将其避免到 false
的方法是使用 ===
运算符,而不是 ==
我的猜测是PHP正在处理它是某种方程式,但似乎有些奇怪。
有人可以详细说明吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
“ 608e-4234”
是 float编号格式< /a>,因此它们在比较时会投入数字。608E-4234
和272E-3063
均为float(0)
,因为它们太小。对于
==
在PHP中,http://php.net/manual/manual/en/language.operators.comparison。 php
注意:
javaScript中的行为又具有
==
和===
?答案是行为与PHP不同。在JavaScript中,如果您将两个值与相同类型进行比较,则
==
与===
相同,因此与两个相同类型的值进行比较不会发生类型cast 。在JavaScript中:
因此,在JavaScript中,当您知道结果的类型时,可以使用
==
而不是====
保存一个字符。例如,
typeof
运算符始终返回字符串,因此您可以使用type foo =='string'
而不是type> type> foo ===='string'string'< /代码>没有损害。
"608E-4234"
is the float number format, so they will cast into number when they compares.608E-4234
and272E-3063
will both befloat(0)
because they are too small.For
==
in php,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:
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 usetypeof foo == 'string'
instead oftypeof foo === 'string'
with no harm.php使用IEEE 754进行浮子,您的数字很小,以至于它们评估为0。
请参阅:
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
我认为PHP将其读为科学语法,将翻译为:
PHP将其解释为
0 = 0
。I think that PHP reads this as a scientific syntax, which will be translated as:
PHP interprets this as being
0 = 0
.PHP将这些字符串作为浮点数进行比较,并且它们都为零,因此您必须使用
====
运算符,PHP is comparing those strings as floating point numbers, and they both are zero, so you MUST use the
===
operator,我正在尝试回答。
如果您使用的是“ ===”,则还使用类型而不是值检查。
如果您使用的是“ ==”,则只需检查值是否相同。
您可以参考 there 和在这里。
I'm trying to answer.
If you are using "===", you also check with the type instead of the value.
If you are using "==", you just check the value is the same or not.
you can reference to here and here.
其他答案已经指出了这一点,但是PHP手册现在已经明确了。 php se se se e e 按数字作为科学符号
如您所见,这是情况不敏感(
e
e e e > e )。在弱类型字符串比较中,这将变成一个gotcha2e1
确实是2 *(10 ^ 1)
,并且可以使用20。将返回预期的false
。 问题从奏效的
到这两个数字都可以由PHP表示(如 jvdberg注意到),因此它们被转换为<<<<<<<<<<<<代码> 0
Other answers have noted this, but the PHP manual has made this explicit now. PHP sees any string with an
E
bounded by numbers as scientific notationAs you can see, this is case insensitive (
E
ore
). Where this becomes a gotcha is in weak type string comparisons2E1
is really2 * (10 ^ 1)
, and that works out to 20. Insert any other letter there and it will return the expectedfalse
. From the questionThat works out to
Neither number can be represented by PHP (as JvdBerg noted), so they are converted to
0
这就是它所看到的:
http://www.wolframalpha.com/input/? a>
由于它们不适合变量,它们都等于0或任何默认值PHP选择的任何默认值,因此是等效的。
This is what it is seeing:
http://www.wolframalpha.com/input/?i=608E-4234&dataset=
http://www.wolframalpha.com/input/?i=272E-3063
As they don't fit into the variable, they both equate to 0, or whatever default value php chooses, and therefore are equivalent.