我正在阅读有关 php 的内容,它说,
==
is Equality such that$a == $b
istrue
if$a
and$b
have the same elements.
===
is Identity such that$a === $b
istrue
if$a
and$b
have the same elements, with the same types, in the same order.
所以,我想我会尝试自己看看差异,然后用这个小脚本编写:
$a = array(1, 2, 3);
$b = array(2, 3, 1);
if ($a==$b) {echo "yeehaw!";} else {echo "nope";}
if ($a===$b) {echo "yup";} else {echo "nope";}
我的想法是,两个数组相等不需要相同的顺序。然而,当我运行这个时,我得到了“nope”和“nope”。
有什么区别?
最佳答案
您提供的数组具有相同的一组值,但键值对不同。
请尝试以下用例(不同顺序的相同键值对):
$a = array(0=>1, 1=>2, 2=>3);
$b = array(1=>2, 2=>3, 0=>1);
...和以下用例(不同的数据类型):
$a = array(1, 2, 3);
$b = array('1', '2', '3');
关于php - php 中 == 和 === WRT 数组的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17638015/