2011-05-05 33 views
33

嘿,如果你有下面的代码,并且想要检查是否$key匹配Hello我发现,如果变量是0,比较总是返回true。我遇到过这个时,一个特殊的密钥数组,并想知道为什么它没有按预期工作。 查看此代码的示例。为什么(0 =='Hello')在PHP中返回true?

$key = 1; 
if ($key != 'Hello') echo 'Hello'; //echoes hello 

$key = 2; 
if ($key != 'Hello') echo 'Hello'; //echoes hello 

$key = 0; 
if ($key != 'Hello') echo '0Hello'; //doesnt echo hello. why? 
if ($key !== 'Hello') echo 'Hello'; //echoes hello 

任何人都可以解释这一点吗?

+5

有趣的是,有多少人没有正确阅读或理解问题。 – 2011-05-05 08:14:19

回答

48

运营商==!=不会比较类型。因此PHP自动将'Hello'转换为一个整数,即0intval('Hello'))。如果不确定类型,请使用类型比较运算符===!==。或者更好的确定你的程序在任何时候处理的是哪种类型。

+2

+1了解您正在处理的类型。不幸的是,类型暗示在API或文档中被忽视。 – ChrisR 2011-05-16 10:29:49

1

几乎任何非零值在PHP后台转换为true。

所以1,2,3,4,“你好”,“世界”,等等都将是等于真实的,而0等于假

的唯一理由!==工作原理是因为它是比较的数据类型是相同的太

1

由于PHP确实自动浇铸比较不同类型的值。您可以在PHP文档中看到table of type-conversion criteria

根据您的情况,字符串"Hello"会根据PHP自动转换为一个数字,即0。因此真正的价值。

如果要比较不同类型的值,你应该使用类型安全的运营商:

$value1 === $value2; 

$value1 !== $value2; 

一般来说,PHP计算结果为零每一个不能被识别字符串作为一个数字。

+0

downvote是因为...? – elitalon 2011-12-01 08:26:49

3

其他人已经很好地回答了这个问题。我只想给出一些其他的例子,你应该知道,所有这些都是由PHP的类型杂耍造成的。以下所有的比较将返回真正

  • 'ABC' == 0
  • 0 == NULL
  • '' == NULL
  • 1 ==“1Y?Z”

,因为我发现这种行为的危险,我写我自己平等的方法,在我的项目中使用它:

/** 
* Checks if two values are equal. In contrast to the == operator, 
* the values are considered different, if: 
* - one value is null and the other not, or 
* - one value is an empty string and the other not 
* This helps avoid strange behavier with PHP's type juggling, 
* all these expressions would return true: 
* 'abc' == 0; 0 == null; '' == null; 1 == '1y?z'; 
* @param mixed $value1 
* @param mixed $value2 
* @return boolean True if values are equal, otherwise false. 
*/ 
function sto_equals($value1, $value2) 
{ 
    // identical in value and type 
    if ($value1 === $value2) 
    $result = true; 
    // one is null, the other not 
    else if (is_null($value1) || is_null($value2)) 
    $result = false; 
    // one is an empty string, the other not 
    else if (($value1 === '') || ($value2 === '')) 
    $result = false; 
    // identical in value and different in type 
    else 
    { 
    $result = ($value1 == $value2); 
    // test for wrong implicit string conversion, when comparing a 
    // string with a numeric type. only accept valid numeric strings. 
    if ($result) 
    { 
     $isNumericType1 = is_int($value1) || is_float($value1); 
     $isNumericType2 = is_int($value2) || is_float($value2); 
     $isStringType1 = is_string($value1); 
     $isStringType2 = is_string($value2); 
     if ($isNumericType1 && $isStringType2) 
     $result = is_numeric($value2); 
     else if ($isNumericType2 && $isStringType1) 
     $result = is_numeric($value1); 
    } 
    } 
    return $result; 
} 

希望这有助于有人做了他的申请更加坚实,原创文章可在这里找到: Equal or not equal

+0

虽然没有读取代码,但函数对'==='运算符的优点是什么? – ZoolWay 2011-05-05 11:37:53

+1

这与使用==运算符的优点相同,即使它们具有不同的类型,也可以比较两个变量。 PHP不能很好地支持你明确地控制类型,并且通常函数不知道参数是否通过,是数字“8”还是用户输入“8”。然后,无论如何你都可以比较它们,你很高兴。否则,你将不得不为函数本身的所有可能的类型编写代码。 – martinstoeckli 2011-05-05 12:14:59

相关问题