Why does (0 == 'Hello') return true in PHP?

Hey, if you have got the following code and want to check if $key matches Hello I've found out, that the comparison always returns true if the variable is 0. I've came across this when an array for a special key and wondered why it's wasn't working as expected. См. Пример в этом коде.

$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

Кто-нибудь может это объяснить?

34
задан Tim 5 May 2011 в 07:49
поделиться