三个不同的等于
=
, ==
和===
什么区别?
我认为使用一个等号是声明一个variables,而两个等号是比较条件,最后三个等号是比较声明variables的值。
你有=
赋值运算符 , ==
'等于'比较运算符和===
'相同'比较运算符 。
$a = $b Assign Sets $a to be equal to $b. $a == $b Equal TRUE if $a is equal to $b. $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
有关==
和===
需要的更多信息,以及每种情况,请查看文档 。
-
=
是赋值运算符 -
==
是比较运算符(检查两个variables是否具有相同的值) -
===
是相同的比较运算符(检查两个variables是否具有相同的值并且是相同的types)。
=赋值运算符
==检查两个variables是否具有相同的值
===检查两个variables是否具有相同的值,以及它们的types是否相同
我知道最简单的解释:
<?php $a = 1; $b = "1"; echo "comparing different types:<br/>"; compare($a, $b); $a = 1; $b = 1; echo "comparing equal types:<br/>"; compare($a, $b); function compare($a, $b) { if($a == $b) echo "a == b<br/>"; if($a === $b) echo "a === b<br/>"; } ?>
你是对的=
是赋值操作符。 另外两个是比较运算符,你可以在这里阅读更多。
也许你可以更好地理解==和===之间的区别,涉及自动投射的例子:
echo '"5 is not a number" == 5'."\n"; if("5 is not a number" == 5) { echo "maybe there is something wrong here\n"; } else { echo " The integer and the string are different\n"; } echo '"5 is not a number" === 5'."\n"; if("5 is not a number" === 5) { echo "maybe there is something wrong here\n"; } else { echo " The integer and the string are different\n"; }
其他人都澄清了…我只是想添加一个例子来澄清它:
<?php $mystring = 'abc'; $findme = 'a'; $pos = strpos($mystring, $findme); // Note our use of ===. Simply == would not work as expected // because the position of 'a' was the 0th (first) character. if ($pos === false) { echo "The string '$findme' was not found in the string '$mystring'"; } else { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; } ?>
对于高级PHP用户,要知道==
和===
之间的区别,并且问自己:“当我确定两个操作数是相同的types时,比较比==
还是更快?
简短而一般的答案是: 在这种情况下使用===
没有性能增益,所以你应该使用==
。
对于那些对自己进行基准testing感兴趣的人,可以使用我编写的以下代码ad-hoc并为$a
和$b
尝试不同的值:
<?php // CONFIGURATION $cycles = 1000000; $a = 'random string 1'; $b = 'random string 2'; // FUNCTIONS function compare_two_equals($a, $b) { if ($a == $b) { return TRUE; } else { return FALSE; } } function compare_three_equals($a, $b) { if ($a === $b) { return TRUE; } else { return FALSE; } } // EXECUTION $time = microtime(TRUE); for ($count_a = 0; $count_a < $cycles; $count_a++) { compare_two_equals($a, $b); } $time_two_a = microtime(TRUE) - $time; $time = microtime(TRUE); for ($count_a = 0; $count_a < $cycles; $count_a++) { compare_three_equals($a, $b); } $time_three_a = microtime(TRUE) - $time; $time = microtime(TRUE); for ($count_a = 0; $count_a < $cycles; $count_a++) { compare_two_equals($a, $b); } $time_two_b = microtime(TRUE) - $time; $time = microtime(TRUE); for ($count_a = 0; $count_a < $cycles; $count_a++) { compare_three_equals($a, $b); } $time_three_b = microtime(TRUE) - $time; $time = microtime(TRUE); // RESULTS PRINTING print "<br />\nCOMPARE == (FIRST TRY): " . number_format($time_two_a, 3) . " seconds"; print "<br />\nCOMPARE == (SECOND TRY): " . number_format($time_two_b, 3) . " seconds"; print "<br />\nCOMPARE === (FIRST TRY): " . number_format($time_three_a, 3) . " seconds"; print "<br />\nCOMPARE === (SECOND TRY): " . number_format($time_three_b, 3) . " seconds"; ?>
注意:只有当每个“第一次尝试”非常接近“第二次尝试”时,比较才有效。 如果它们明显不同,则意味着处理器在执行比较时忙于做其他事情,所以结果不可靠,基准应该再次运行。
以下区别=。 ==。 和===:
=赋值运算符,
=='等于'比较运算符和
==='完全相同的'比较运算符。
$a = $b
分配设置$ a等于$ bie $a = 5;
所以$赋值5。
$a == $b
等于TRUE如果$ a等于$ bie if($a==5)
在这里我们看到我们正在使用==比较值。
$a === $b
如果$ a等于$ b,则它是相同的,并且它们是相同的types。 (在PHP 4中引入)即($a ===5)
这也用于比较,但它也比较值,以及数据types。 这意味着$a = '5'
和$a = 5
是不一样的。 $a = '5'
表示$赋值string'5', $a=5
赋值整数5值。