How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?

前端 未结 11 2122
故里飘歌
故里飘歌 2020-11-22 16:52

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

What would be some useful examples?


Additionally, how are these operators us

11条回答
  •  情歌与酒
    2020-11-22 17:11

    The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.

    Examples:

    1 === 1: true
    1 == 1: true
    1 === "1": false // 1 is an integer, "1" is a string
    1 == "1": true // "1" gets casted to an integer, which is 1
    "foo" === "foo": true // both operands are strings and have the same value

    Warning: two instances of the same class with equivalent members do NOT match the === operator. Example:

    $a = new stdClass();
    $a->foo = "bar";
    $b = clone $a;
    var_dump($a === $b); // bool(false)
    

提交回复
热议问题