2011-05-05 35 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

相關問題