PHP 中的 == 运算符具有传递性吗?
在 JavaScript 中,==
运算符不一定具有传递性:
js> '0' == 0
true
js> 0 == ''
true
js> '0' == ''
false
在 PHP 中也是如此吗?你能举个例子吗?
In JavaScript, the ==
operator isn't necessarily transitive:
js> '0' == 0
true
js> 0 == ''
true
js> '0' == ''
false
Is the same true in PHP? Can you give an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
否,
==
运算符不具有传递性。完全相同的场景在 PHP 中给出相同的结果。
产量:
No, the
==
operator is not transitive.The exact same scenario gives the same result in PHP.
yields:
PHP也是如此:
你自己没有测试过吗?这些与您为 javascript 提供的语句相同。
The same is true in PHP:
Did you not test it yourself? These are the same statements you provided for javascript.
脚本语言的问题在于我们开始以非严格的方式比较事物,这导致了不同的“平等”感。当你比较“0”和0时,你的意思与比较“0”和NULL时不同。因此,这些运算符不具有传递性是有道理的。然而,自反性应该是一个不变量。根据定义,平等是自反的。不管你所说的平等是什么意思,A 等于它自己应该总是正确的。
另一个更明显的:
The problem with scripting languages is that we start comparing things in a non-strict way, which leads to different senses of "equality." when you are comparing "0" and 0, you mean something different then when you are comparing "0" and NULL. Thus it makes sense that these operators would not be transitive. Reflexivity however, should be an invariant. Equality is by definition reflexive. No matter what sense you mean equality, it should always be true that A equals itself.
another more obvious one: