PHP字符串比较在不匹配时返回true
问题描述:
为什么以下字符串比较返回true?PHP字符串比较在不匹配时返回true
我正在使用Laravel框架4.2,Form :: select使用此方法比较选项值,它找到不应该是的匹配项。
public function testStringComparison1()
{
$value = '8000003E-1408739159';
$selected = '8000010E-1434656519';
$b = false;
if ((string) $value == (string) $selected) {
$b = true;
}
$this->assertFalse($b); // this fails, $b == true
}
public function testStringComparison2()
{
$value = '8000008E-1408740949';
$selected = '8000010E-1434656519';
$b = false;
if ((string) $value == (string) $selected) {
$b = true;
}
$this->assertFalse($b); // this fails, $b == true
}
总是输入杂耍。使用'===' – AbraCadaver
感谢您的回应。为了跟进并更好地理解,PHP可以比较哪些“类型”以确定它们是否相等? – user3720435
它们是浮点数的字符串表示形式:'echo(float)$ value; echo(float)$ selected;'http://php.net/manual/en/language.types.string.php#language.types.string.conversion – AbraCadaver