为什么这两个对象是相等的(==)?

在PHP中,我有两个对象,它们因$ _frets变量而异(x是一个字符串“x”),但PHP说

($o1 == $o2) == TRUE.

为什么?

$o1转储:

guitarChord Object
(
    [_guitarChord:guitarChord:private] => 
    [_chord:guitarChord:private] => chord Object()
    [_baseFret:guitarChord:private] => 0
    [_frets:guitarChord:private] => Array
        (
            [0] => x
            [1] => 0
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => x
        )

    [_tuning:guitarChord:private] => tuning Object()
    [currVariation] => 0
    [nextVariation] => 
    [prevVariation] => 
)

$o2转储:

guitarChord Object
(
    [_guitarChord:guitarChord:private] => 
    [_chord:guitarChord:private] => chord Object()
    [_baseFret:guitarChord:private] => 0
    [_frets:guitarChord:private] => Array
        (
            [0] => x
            [1] => 0
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 0
        )

    [_tuning:guitarChord:private] => tuning Object()
    [currVariation] => 0
    [nextVariation] => 
    [prevVariation] => 
)

编辑:

所以原因是因为("x" == 0) = TRUE 。 谁能告诉我为什么?


x代表一个字符串还是x代表null? 如果它表示为空,那么这就是我的理论:比较运算符页面有例1中数组比较算法的转录。 基于这一点,我想你的情况下,最终会发生什么是0和null之间的比较。 根据上面的表格,当null与任何东西比较时,它都被转换为bool。 所以你最终将0(false)与null(false)进行比较,导致两个数组被认为是相等的。

链接地址: http://www.djcxy.com/p/52861.html

上一篇: Why this two objects are equal (==)?

下一篇: How to solve Multiple definition errors in gcc linux?