==和===的区别是什么?
松==比较究竟是如何工作的? 严格的===比较到底是如何工作的?
有什么有用的例子吗?
==和===的区别是什么?
松==比较究竟是如何工作的? 严格的===比较到底是如何工作的?
有什么有用的例子吗?
当前回答
您可以使用===来测试函数或变量是否为假,而不仅仅是等于假(零或空字符串)。
$needle = 'a';
$haystack = 'abc';
$pos = strpos($haystack, $needle);
if ($pos === false) {
echo $needle . ' was not found in ' . $haystack;
} else {
echo $needle . ' was found in ' . $haystack . ' at location ' . $pos;
}
在这种情况下,strpos将返回0,这在测试中等同于false
if ($pos == false)
or
if (!$pos)
这不是你想要的。
其他回答
到目前为止,所有的答案都忽略了一个危险的问题===。这里已经顺便提过,但没有强调,integer和double是不同的类型,所以代码如下:
$n = 1000;
$d = $n + 0.0e0;
echo '<br/>'. ( ($n == $d)?'equal' :'not equal' );
echo '<br/>'. ( ($n === $d)?'equal' :'not equal' );
给:
equal
not equal
请注意,这不是一个“舍入误差”的情况。这两个数字直到最后一位都完全相等,但它们的类型不同。
This is a nasty problem because a program using === can run happily for years if all of the numbers are small enough (where "small enough" depends on the hardware and OS you are running on). However, if by chance, an integer happens to be large enough to be converted to a double, its type is changed "forever" even though a subsequent operation, or many operations, might bring it back to a small integer in value. And, it gets worse. It can spread - double-ness infection can be passed along to anything it touches, one calculation at a time.
在现实世界中,例如,在处理2038年之后的日期的程序中,这可能是一个问题。此时,UNIX时间戳(1970-01-01 00:00:00 UTC以来的秒数)需要超过32位,因此在某些系统上,它们的表示将“神奇地”切换为两倍。因此,如果你计算两次时间的差值,你可能会得到几秒,但结果是双倍,而不是2017年的整数结果。
我认为这比字符串和数字之间的转换更糟糕,因为它很微妙。我发现记录什么是字符串,什么是数字很容易,但是记录数字中的比特数却超出了我的能力。
因此,在上面的答案中有一些不错的表,但在1(作为整数)和1(微妙的双精度)和1.0(明显的双精度)之间没有区别。另外,总是使用===而从不使用==的建议也不是很好,因为===有时会在==正常工作的地方失败。此外,JavaScript在这方面是不等效的,因为它只有一种数字类型(在内部它可能有不同的位表示,但它不会导致===的问题)。
我的建议是——两者都不要用。你需要编写自己的比较函数来解决这个问题。
关于JavaScript:
===操作符的工作原理与==操作符相同,但它要求其操作数不仅具有相同的值,而且具有相同的数据类型。
例如,下面的示例将显示'x和y是相等的',而不是'x和y是相同的'。
var x = 4;
var y = '4';
if (x == y) {
alert('x and y are equal');
}
if (x === y) {
alert('x and y are identical');
}
您可以使用===来测试函数或变量是否为假,而不仅仅是等于假(零或空字符串)。
$needle = 'a';
$haystack = 'abc';
$pos = strpos($haystack, $needle);
if ($pos === false) {
echo $needle . ' was not found in ' . $haystack;
} else {
echo $needle . ' was found in ' . $haystack . ' at location ' . $pos;
}
在这种情况下,strpos将返回0,这在测试中等同于false
if ($pos == false)
or
if (!$pos)
这不是你想要的。
在PHP数组和对象中,==和===之间有两个没有人提到的区别:两个具有不同键排序的数组和对象。
两个具有不同键排序的数组
如果你有两个数组,它们的键排序不同,但有相同的键值映射,它们是严格不同的(即使用===)。这可能会导致问题,如果你对一个数组进行键排序,并试图将排序后的数组与原始数组进行比较。
例如:
$arrayUnsorted = [
"you" => "you",
"I" => "we",
];
$arraySorted = $arrayUnsorted;
ksort($arraySorted);
$arrayUnsorted == $arraySorted; // true
$arrayUnsorted === $arraySorted; // false
对象
请记住,主要规则是两个不同的对象永远不会严格相等。请看下面的例子:
$stdClass1 = new stdClass();
$stdClass2 = new stdClass();
$clonedStdClass1 = clone $stdClass1;
$stdClass1 == $stdClass2; // true
$stdClass1 === $stdClass2; // false
$stdClass1 == $clonedStdClass1; // true
$stdClass1 === $clonedStdClass1; // false
注意:将一个对象赋值给另一个变量并不会创建一个副本——相反,它创建了对同一对象的引用。在这里看到的。
注意:从PHP7开始,引入了匿名类。在上面的测试中,新类{}和新stdClass()之间没有区别。
这都是关于数据类型的。以BOOL (true或false)为例:
True也等于1和 False也等于0
==在比较时不关心数据类型: 所以如果你有一个变量是1(这也可能是真的):
$ var = 1;
然后与==进行比较:
if ($var == true)
{
echo"var is true";
}
但$var并不等于true,对吧?它的int值为1,而int值为true。
使用===,检查数据类型,以确保两个变量/对象/无论使用相同的类型。
如果我这样做了
if ($var === true)
{
echo "var is true";
}
这个条件不会为真,因为$var !== true它只== true(如果你知道我的意思)。
你为什么需要这个?
简单-让我们来看看PHP的一个函数:array_search():
array_search()函数的作用是:简单地在数组中搜索一个值,并返回该值所在元素的键。如果在数组中找不到该值,则返回false。但是,如果对存储在数组的第一个元素(数组键值为0)....中的值执行array_search()会怎样呢array_search()函数将返回0…等于false..
所以如果你有:
$arr = array("name");
if (array_search("name", $arr) == false)
{
// This would return 0 (the key of the element the val was found
// in), but because we're using ==, we'll think the function
// actually returned false...when it didn't.
}
所以,你现在明白这为什么会成为一个问题了吗?
大多数人在检查函数是否返回false时不使用== false。相反,他们使用!但实际上,这与使用==false完全相同,所以如果你这样做:
$arr = array("name");
if (!array_search("name", $arr)) // This is the same as doing (array_search("name", $arr) == false)
因此,对于这种情况,您可以使用===来代替,以便检查数据类型。