提问人:Strawberry 提问时间:1/14/2010 最后编辑:RahulStrawberry 更新时间:6/4/2019 访问量:49679
3 个不同的等价物
The 3 different equals
答:
179赞
gnarf
1/14/2010
#1
您有赋值运算符、“相等”比较运算符和“相同”比较运算符。=
==
===
$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)
有关 和 的需要以及使用每个 的情况的详细信息,请查看文档。==
===
评论
0赞
stloc
1/8/2016
当我比较两个相同的日期时间时,我有错误的结果,为什么?示例:sandbox.onlinephpfunctions.com/code/...
4赞
mystery
1/28/2016
@stloc,with objects 会告诉您它们是否是同一个对象,而不是它们是否具有相同的内容。===
0赞
mvorisek
7/13/2018
在 PHP4 中引入,在 2018 年很有趣 ;-)
9赞
Silvio Donnini
1/14/2010
#2
= 赋值运算符
== 检查两个变量是否具有相同的值
=== 检查两个变量是否具有相同的值,以及它们的类型是否相同
评论
1赞
Phil Perry
8/22/2013
还要了解两个比较运算符的 != 和 !== 'not' 版本。有些语言使用 := 作为赋值,只是为了避免这种混淆。
23赞
Rich Adams
1/14/2010
#3
=
是赋值运算符==
是比较运算符(检查是否 两个变量的值相等)===
是相同的比较 运算符(检查是否有两个变量 具有相等的值并且具有相同的值 类型)。
0赞
lucaferrario
7/8/2013
#4
对于高级PHP用户来说,知道和之间的区别并问自己“当我确定两个操作数是同一类型时,与或与相比是否更快?==
===
==
===
简短而笼统的答案是:在这种情况下,使用 ===
不会提高性能,因此您可能应该使用 ==
。
对于那些有兴趣自己进行基准测试的人,您可以使用我临时编写的以下代码,并为 和 尝试不同的值:$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";
?>
注意:仅当每个“第一次尝试”都非常接近其“第二次尝试”时,比较才有效。如果它们明显不同,则意味着处理器在执行比较时忙于做其他事情,因此结果不可靠,应再次运行基准测试。
评论
9赞
gnarf
11/12/2013
像这样的微基准测试不是很可靠。您也极不可能担心或成为性能问题的原因。IMO:最好是严格(),除非你明确想对你的比较宽松()。奇怪的边缘情况的数量,即 可能会导致古怪的错误。 从来没有这个问题。==
===
===
==
"5 is not a number" == 5
===
0赞
lucaferrario
2/18/2016
我的测试是告诉程序员,如果他们出于性能原因而选择,他们就错了。因此,程序员可以自由选择或基于逻辑原因,而不是基于性能原因:有不同的情况可以选择其中之一,并且根本不必须考虑性能。===
===
==
3赞
Gideon Babu
2/25/2019
#5
= 运算符将值分配给变量 $six = 6;值 6 分配给变量 $six
== 运算符检查两个变量的值是否相等,并且主要用于 if 语句等条件
$a = 2;
$b = 2;
if ($a == $b) {
echo both variables have the same value;
}
=== 类似于 == 的运算符(检查值是否相等),并检查两者的数据类型是否相同
$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';
}
这里$a是 int 类型,而 $b 是字符串类型。所以这里的输出
上一个:MongoDB 元组比较(有序)
评论
=