What does === do in PHP

后端 未结 8 1586
天涯浪人
天涯浪人 2021-01-05 23:25

I have been programming in PHP for a while but I still dont understand the difference between == and ===. I know that = is assignment. And == is equals to. So what is the pu

相关标签:
8条回答
  • 2021-01-06 00:12

    It's a true equality comparison.

    "" == False for instance is true.

    "" === False is false

    0 讨论(0)
  • 2021-01-06 00:23

    It is true that === compares both value and type, but there is one case which hasn't been mentioned yet and that is when you compare objects with == and ===.

    Given the following code:

    class TestClass {
      public $value;
    
      public function __construct($value) {
        $this->value = $value;
      }
    }
    
    $a = new TestClass("a");
    $b = new TestClass("a");
    
    var_dump($a == $b);  // true
    var_dump($a === $b); // false
    

    In case of objects === compares reference, not type and value (as $a and $b are of both equal type and value).

    0 讨论(0)
提交回复
热议问题