php 3个不同的等于
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2063480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The 3 different equals
提问by Strawberry
What is the difference between =, ==, and ===?
是什么区别=,==和===?
I think using one equal sign is to declare a variable while two equal signs are for a comparison condition and lastly three equal signs are for comparing values of declared variables.
我认为使用一个等号是声明一个变量,而两个等号用于比较条件,最后三个等号用于比较声明变量的值。
回答by gnarf
You have =the assignment operator, ==the 'equal' comparison operatorand ===the 'identical' comparison operator.
你=的赋值操作符,==在“平等”比较操作和===对“相同”的比较操作。
$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)
For more info on the need for ==and ===, and situations to use each, look at the docs.
有关==和的需要以及===使用每个的情况的更多信息,请查看文档。
回答by Rich Adams
=is the assignment operator==is the comparison operator (checks if two variables have equal values)===is the identical comparison operator (checks if two variables have equal values and are of the same type).
=是赋值运算符==是比较运算符(检查两个变量是否具有相等的值)===是相同的比较运算符(检查两个变量是否具有相同的值并且是相同的类型)。
回答by Silvio Donnini
= assignment operator
= 赋值运算符
== checks if two variables have the same value
== 检查两个变量是否具有相同的值
=== checks if two variables have the same value AND if their types are the same
=== 检查两个变量是否具有相同的值以及它们的类型是否相同
回答by Gideon Babu
The = operator assigns the value to a variable $six = 6; value 6 is assigned to variable $six
= 运算符将值赋给变量 $six = 6; 值 6 分配给变量 $6
== operator check if the value of both variables is equal and mostly used in conditions like if statements
== 运算符检查两个变量的值是否相等,主要用于 if 语句等条件
$a = 2;
$b = 2;
if ($a == $b) {
echo both variables have the same value;
}
=== operator similar to == (check if the value equals) and also check if both of same data type
=== 类似于 == 的运算符(检查值是否相等),并检查两者是否具有相同的数据类型
$a = 2;
$b = "2";
if ($a === $b) {
echo "both variable have same value and of same data type";
} else {
echo 'both variable is either not equal or not of same data type';
}
// here $a is of type int whereas $b is of type string. So here the output
// 这里 $a 是 int 类型,而 $b 是 string 类型。所以这里的输出
回答by lucaferrario
For advanced PHP users, knowing the difference between ==and ===and asking themselves "is it faster to compare with ==or with ===when I'm sure that both the operands are the same type?"
对于高级PHP用户,知道之间的差别==和===与问自己“是它更快地进行比较==或与===我敢肯定,这两个操作数是相同的类型?”
The short and general answer is: There is no performance gain in using ===in this cases, so you should probably use ==.
简短而通用的答案是:在这种情况下使用没有性能提升===,因此您可能应该使用==.
For the ones interested in benchmarking it themselves, you can use the following code I wrote ad-hoc and try different values for $aand $b:
对于那些对自己进行基准测试感兴趣的人,您可以使用我临时编写的以下代码,并尝试为$aand尝试不同的值$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";
?>
NOTE: The comparison is valid only when each "FIRST TRY" is very close to its "SECOND TRY". If they are significantly different, it means that the processor was busy doing something else while executing the comparisons and so the results are unreliable and the benchmark should be run again.
注意:只有当每个“第一次尝试”非常接近其“第二次尝试”时,比较才有效。如果它们显着不同,则意味着处理器在执行比较时忙于做其他事情,因此结果不可靠,应该再次运行基准测试。

