PHP中相同的字符串不相同

时间:2017-10-02 13:43:25

标签: php

class VocabularyValidator
{
    public function __construct()
    {
        $this->check('termIdenfifier');
    }

    public function check ($tst) {

        var_dump($tst);

        $a = (string)'a:termIdentifier';
        $b = sprintf('a:%s', (string)$tst);

        var_dump($a);
        var_dump($b);

        var_dump(bin2hex($a));
        var_dump(bin2hex($b));

        var_dump(strcmp($a, $b));
        var_dump($a === $b);
    }

    public static function check2 ($tst) {

        var_dump($tst);

        $a = (string)'a:termIdentifier';
        $b = sprintf('a:%s', (string)$tst);

        var_dump($a);
        var_dump($b);

        var_dump(bin2hex($a));
        var_dump(bin2hex($b));

        var_dump(strcmp($a, $b));
        var_dump($a === $b);
    }
}

我在控制器中这样称呼它:

VocabularyValidator::check2('termIdentifier');
new VocabularyValidator();

输出:

string 'termIdentifier' (length=14)
string 'a:termIdentifier' (length=16)
string 'a:termIdentifier' (length=16)
string '613a7465726d4964656e746966696572' (length=32)
string '613a7465726d4964656e746966696572' (length=32)
int 0
boolean true

string 'termIdenfifier' (length=14)
string 'a:termIdentifier' (length=16)
string 'a:termIdenfifier' (length=16)
string '613a7465726d4964656e746966696572' (length=32)
string '613a7465726d4964656e666966696572' (length=32)
int 14
boolean false

为什么相同的功能会产生不同的结果?

1 个答案:

答案 0 :(得分:4)

您有拼写错误termIdentifier!== termIdenfifier

$this->check('termIdenfifier');