Is the == operator transitive in PHP?

◇◆丶佛笑我妖孽 提交于 2019-12-19 16:51:13

问题


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?


回答1:


No, the == operator is not transitive.

The exact same scenario gives the same result in PHP.

echo var_dump('0'==0);
echo var_dump(0=='');
echo var_dump('0'=='');

yields:

boolean true
boolean true
boolean false 



回答2:


The same is true in PHP:

//php

'0'==0  //true
0==''   //true
''=='0' //false

Did you not test it yourself? These are the same statements you provided for javascript.




回答3:


array() == NULL // true
0 == NULL       // true
array() == 0    // false

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:

true == 1 // true
true == 2 // true
1 == 2    // false


来源:https://stackoverflow.com/questions/4753759/is-the-operator-transitive-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!