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 do NOT match the === operator. Example:
$a = new stdClass();
$a->foo = "bar";
$b = clone $a;
var_dump($a === $b); // bool(false)
=== will only return true if both operands are the same type and the values are equal ===必须值跟类型都一样才返回true
== 值相等的情况下就返回true
本文详细解释了PHP中==与===操作符的区别。==操作符会在比较前进行类型转换,而===则进行类型安全的比较,只有当两个操作数类型相同且值相等时才返回true。文章通过实例展示了不同类型数据在使用这两种操作符时的行为差异。

被折叠的 条评论
为什么被折叠?



