Oli Oli - 1 year ago 125
PHP Question

Identical strings not equal in 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);
}
}


I call this in a Controller like this:

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


The output:

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


Why are identical functions giving different results?

Answer Source

You have a typo termIdentifier !== termIdenfifier

$this->check('termIdenfifier');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download