所有 PHP 相等比较都是对称的吗?
Is $a == $b
always equivalent to $b == $a
?
I think in JavaScript there are a few weird cases where that's not true, due to casting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简而言之,是的。
$a == $b
始终等价于$b == $a
。有一些缺点,例如浮动。当然,无论如何,您都不应该嵌套两个浮点数以实现相等。编辑
关于浮动:如果您有两个浮动并比较它们,那么它们在技术上应该是相同的。然而,看似具有相同值的浮点值实际上并不需要相同。因此,如果
$a
是文字.69
而$b
是计算结果,它们很可能不同,但两者显示相同的值。这就是为什么您永远不应该使用==
来比较浮点值。如果您需要比较浮点值,您确实需要在特定情况下使用最小的可接受差异。像这样的东西可以用于比较浮点数(将我们可接受的最小差异设置为
0.000001
):PHP:abs - 绝对值
In short, yes.
$a == $b
will always be the equivalent of$b == $a
. There are some short comings, such as floats. Of course, you shouldn't be nesting two float for equality anyways.EDIT
Concerning floats: If you had two float and compared them, they technically should be the same. However, floating point values which seem to have the same value do not need to actually be identical. So, if
$a
is a literal.69
and$b
is the result of a calculation, they can very well be different, but both display the same value. This is why you should never compare floating-point values by using the==
.If you need to compare floating-point values, you really need to use the smallest acceptable difference in your specific case. Something like this would work for comparing floats (setting our smallest acceptable difference at
0.000001
):PHP: abs - Absolute Value
我能看到的唯一不同的类型是这样的:
要了解原因,请查看它
如果首先运行
A
,结果将是2
和A
的结果code>B 将是 2,因此它们相等并且测试将为true
。如果先运行
B
,则结果将为1
,而B
的结果将为2,因此它们不相等,测试将是假
。但对于任何单一类型比较(其中
A
是变量而不是表达式),它始终是自反的。因此,从一般意义上来说,
A == B
并不总是 100% 保证等于B == A
。对于变量来说,它总是等价的。但对于涉及变量赋值或修改的复杂表达式,情况可能并非如此。The only type that I could see being different is something like:
To see why, look at it
If
A
is run first, the result will be2
and the result ofB
will be 2, so they are equal and the test will betrue
.If
B
is run first, the result will be1
, and the result ofB
will be 2, so they are not equal and the test will befalse
.But for any single type comparison (Where
A
is a variable and not an expression) it will always be reflexive.So in the general sense,
A == B
is not always 100% guaranteed to be equivalent toB == A
. For variables, it will always be equivalent. But for complex expressions involving assignment or modification of variables it may not be.取决于这两个调用之间发生的情况。否则是的,这些都是一样的。顺序没有区别。使用 2 equals
==
1 的字符串和 1 的整数,比较时将返回 true。类型被忽略,仅比较值。所以没有什么奇怪的。http://php.net/manual/en/language.operators.comparison.php
输出:1
http://www.ideone.com/JLJWQ
编辑< /b>
需要澄清的是,您绝对无法在 $a 或 $b 中放入任何内容来获得不同的比较输出,只需将其放在运算符的另一侧即可。
对于任何 $a 或 $b 值,其输出毫无疑问总是 true true 或 false false。
Depends what happens between those two calls. Otherwise yes, those are the same. The order makes no difference. Using 2 equals
==
A string of 1 and integer of 1, will return true when compared. Type is ignored, only value is compared. So no wierdness.http://php.net/manual/en/language.operators.comparison.php
Outputs: 1
http://www.ideone.com/JLJWQ
EDIT
To clarify, there is absolutely nothing you can ever put in $a or $b to get a different output on the comparison, just by putting it on the other side of the operator.
The output of that, for any $a or $b values, will always without a doubt be true true, or false false.
http://php.net/manual/en/language.operators.comparison.php
如果您想在比较中考虑类型转换,可以使用不同的运算符。
==
在相等的值上求值为 true,但不比较数据类型。当值和数据类型相等时,===
计算结果为 true。使用后者会考虑通常被忽略的类型转换(例如:表示整数的字符串和正在比较的整数。)条件中的逻辑顺序不应产生差异。
http://php.net/manual/en/language.operators.comparison.php
There are diffrent operators you can use if you want to consider type casting in the comparison.
==
evaluates as true on equal value, but does not compare data type.===
evaluates as true when values are equal as well as datatypes. Using the latter considers type casting where it would normally be ignored (eg: string that represents an integer and an integer being compared.)The order of the logic in the conditional should not make a difference.
我尝试了多种变体,但找不到
($a == $b) !== ($b == $a)
的情况,但到目前为止没有一个起作用:所以,我给出此时。欢迎提出想法!
I have tried a number of variations and cannot find a case where
($a == $b) !== ($b == $a)
but none so far have worked:So, I give up at this point. Ideas welcome!