How do the PHP equality (== double equals) and ide

2018-12-31 00:02发布

What is the difference between == and ===?

  • How exactly does the loosely == comparison work?
  • How exactly does the strict === comparison work?

What would be some useful examples?

20条回答
裙下三千臣
2楼-- · 2018-12-31 00:33

An addition to the other answers concerning object comparison:

== compares objects using the name of the object and their values. If two objects are of the same type and have the same member values, $a == $b yields true.

=== compares the internal object id of the objects. Even if the members are equal, $a !== $b if they are not exactly the same object.

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
查看更多
初与友歌
3楼-- · 2018-12-31 00:33

The === operator is supposed to compare exact content equality while the == operator would compare semantic equality. In particular it will coerce strings to numbers.

Equality is a vast subject. See the Wikipedia article on equality.

查看更多
登录 后发表回答