==和===的区别是什么?

松==比较究竟是如何工作的? 严格的===比较到底是如何工作的?

有什么有用的例子吗?


当前回答

一图胜千言万语:

PHP双等号==等号图表:

PHP三重等号===等号图表:

创建这些图像的源代码:

https://github.com/sentientmachine/php_equality_charts

大师冥想

那些希望保持理智的人,不要再读下去了,因为这些都没有任何意义,只能说PHP的疯狂分形就是这样设计的。

NAN != NAN but NAN == true. == will convert left and right operands to numbers if left is a number. So 123 == "123foo", but "123" != "123foo" A hex string in quotes is occasionally a float, and will be surprise cast to float against your will, causing a runtime error. == is not transitive because "0"== 0, and 0 == "" but "0" != "" PHP Variables that have not been declared yet are false, even though PHP has a way to represent undefined variables, that feature is disabled with ==. "6" == " 6", "4.2" == "4.20", and "133" == "0133" but 133 != 0133. But "0x10" == "16" and "1e3" == "1000" exposing that surprise string conversion to octal will occur both without your instruction or consent, causing a runtime error. False == 0, "", [] and "0". If you add 1 to number and they are already holding their maximum value, they do not wrap around, instead they are cast to infinity. A fresh class is == to 1. False is the most dangerous value because False is == to most of the other variables, mostly defeating it's purpose.

希望:

如果你正在使用PHP,你不应该使用双等号操作符,因为如果你使用三等号,唯一需要担心的边缘情况是NAN和太接近其数据类型最大值的数字,它们会被强制转换为无穷大。使用双等号,任何东西都可以是surprise ==对任何东西或,或可以是出乎你的意愿和!=对显然应该是相等的东西。

在PHP中使用==的任何地方都是糟糕的代码气味,因为隐式强制转换规则暴露了85个错误,这些规则似乎是由数百万程序员通过布朗运动编程设计的。

其他回答

您可以使用===来测试函数或变量是否为假,而不仅仅是等于假(零或空字符串)。

$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)

这不是你想要的。

关于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');
}

PHP双等号==:

在大多数编程语言中,比较操作符(==)一方面检查数据类型,另一方面检查变量的内容是否相等。PHP中的标准比较运算符(==)表现不同。这将尝试在比较之前将两个变量转换为相同的数据类型,然后才检查这些变量的内容是否相同。得到以下结果:

<?php
    var_dump( 1 == 1 );     // true
    var_dump( 1 == '1' );   // true
    var_dump( 1 == 2 );     // false
    var_dump( 1 == '2' );   // false
    var_dump( 1 == true );  // true
    var_dump( 1 == false ); // false
?>

PHP三重等于===:

此操作符还检查变量的数据类型,仅当两个变量具有相同的内容和相同的数据类型时才返回(bool)true。因此,以下是正确的:

<?php
    var_dump( 1 === 1 );     // true
    var_dump( 1 === '1' );   // false
    var_dump( 1 === 2 );     // false
    var_dump( 1 === '2' );   // false
    var_dump( 1 === true );  // false
    var_dump( 1 === false ); // false
?>

更多内容请参见PHP中==和===的区别

在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()之间没有区别。

关于对象比较的其他答案的补充:

==使用对象的名称和它们的值来比较对象。如果两个对象具有相同的类型并且具有相同的成员值,$a == $b的结果为true。

===比较对象的内部对象id。即使成员是相等的,如果它们不是完全相同的对象,$a !== $b。

class TestClassA {
    public $a;
}

class TestClassB {
    public $a;
}

$a1 = new TestClassA();
$a2 = new TestClassA();
$b = new TestClassB();

$a1->a = 10;
$a2->a = 10;
$b->a = 10;

$a1 == $a1;
$a1 == $a2;  // Same members
$a1 != $b;   // Different classes

$a1 === $a1;
$a1 !== $a2; // Not the same object