为什么PHP将带有字母E的字符串转换为一个数字?

发布于 2025-01-26 07:17:28 字数 244 浏览 3 评论 0 原文

为什么以下语句返回 true

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

我还尝试了围绕字符串的单语引号。我唯一可以将其避免到 false 的方法是使用 === 运算符,而不是 ==

我的猜测是PHP正在处理它是某种方程式,但似乎有些奇怪。

有人可以详细说明吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

柠檬色的秋千 2025-02-02 07:17:28

“ 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中:

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

因此,在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 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 from 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.

陈独秀 2025-02-02 07:17:28

php使用IEEE 754进行浮子,您的数字很小,以至于它们评估为0。

请参阅:

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       

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       
ヤ经典坏疍 2025-02-02 07:17:28

我认为PHP将其读为科学语法,将翻译为:

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

PHP将其解释为 0 = 0

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.

情丝乱 2025-02-02 07:17:28

PHP将这些字符串作为浮点数进行比较,并且它们都为零,因此您必须使用 ==== 运算符,

PHP is comparing those strings as floating point numbers, and they both are zero, so you MUST use the === operator,

债姬 2025-02-02 07:17:28

我正在尝试回答。
如果您使用的是“ ===”,则还使用类型而不是值检查。
如果您使用的是“ ==”,则只需检查值是否相同。

您可以参考 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.

思慕 2025-02-02 07:17:28

其他答案已经指出了这一点,但是PHP手册现在已经明确了。 php se se se e e 按数字作为科学符号

exponent_dnum(({{lnum} | {dnum})[ee] [+ - ]?{lnum})

如您所见,这是情况不敏感( e e e e > e )。在弱类型字符串比较中,这将变成一个gotcha

var_dump("2E1" == "020"); // true

2e1 确实是 2 *(10 ^ 1),并且可以使用20。将返回预期的 false 。 问题

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

从奏效的

608 * (10 ^ -4234) == 272 * (10 ^ -3063)

到这两个数字都可以由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 notation

EXPONENT_DNUM (({LNUM} | {DNUM}) [eE][+-]? {LNUM})

As you can see, this is case insensitive (E or e). Where this becomes a gotcha is in weak type string comparisons

var_dump("2E1" == "020"); // true

2E1 is really 2 * (10 ^ 1), and that works out to 20. Insert any other letter there and it will return the expected false. From the question

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

That works out to

608 * (10 ^ -4234) == 272 * (10 ^ -3063)

Neither number can be represented by PHP (as JvdBerg noted), so they are converted to 0

小兔几 2025-02-02 07:17:28

这就是它所看到的:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文