[英]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
.嘿,如果您有以下代码并想检查
$key
是否匹配Hello
我发现,如果变量为0
,比较总是返回true
。 I've came across this when an array for a special key and wondered why it's wasn't working as expected.当一个特殊键的数组时,我遇到了这个问题,并想知道为什么它没有按预期工作。 See this code for an example.
有关示例,请参见此代码。
$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
Can anyone explain this?谁能解释一下?
The operators ==
and !=
do not compare the type.运算符
==
和!=
不比较类型。 Therefore PHP automatically converts 'Hello' to an integer which is 0
( intval('Hello')
).因此 PHP 自动将 'Hello' 转换为 integer ,即
0
( intval('Hello')
)。 When not sure about the type, use the type-comparing operators ===
and !==
.如果不确定类型,请使用类型比较运算符
===
和!==
。 Or better be sure which type you handle at any point in your program.或者更好地确定您在程序中的任何时候处理的类型。
Others have already answered the question well.其他人已经很好地回答了这个问题。 I only want to give some other examples, you should be aware of, all are caused by PHP's type juggling.
我只是想举一些其他的例子,你应该知道,都是由PHP的类型杂耍引起的。 All the following comparisons will return true :
以下所有比较都将返回true :
Because i found this behaviour dangerous, i wrote my own equal method and use it in my projects:因为我发现这种行为很危险,所以我编写了自己的 equal 方法并在我的项目中使用它:
/**
* 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;
}
Hope this helps somebody making his application more solid, the original article can be found here: Equal or not equal希望这可以帮助某人使他的应用程序更可靠,原始文章可以在这里找到:等于或不等于
pretty much any non-zero value gets converted to true in php behind the scenes.在幕后的 php 中,几乎所有非零值都会转换为 true。
so 1, 2,3,4, 'Hello', 'world', etc would all be equal to true, whereas 0 is equal to false所以 1、2、3、4、'Hello'、'world' 等都等于 true,而 0 等于 false
the only reason !== works is cause it is comparing data types are the same too !== 起作用的唯一原因是因为它比较的数据类型也相同
Because PHP does an automatic cast to compare values of different types.因为 PHP 会自动转换来比较不同类型的值。 You can see a table of type-conversion criteria in PHP documentation.
您可以在 PHP 文档中查看类型转换标准表。
In your case, the string "Hello"
is automatically converted to a number, which is 0
according to PHP.在您的情况下,字符串
"Hello"
会自动转换为数字,根据 PHP,该数字为0
。 Hence the true value.因此,真正的价值。
If you want to compare values of different types you should use the type-safe operators:如果你想比较不同类型的值,你应该使用类型安全的操作符:
$value1 === $value2;
or或者
$value1 !== $value2;
In general, PHP evaluates to zero every string that cannot be recognized as a number.通常,PHP 将每个无法识别为数字的字符串计算为零。
In php, the string "0" is converted to the boolean FALSE http://php.net/manual/en/language.types.boolean.php In php, the string "0" is converted to the boolean FALSE http://php.net/manual/en/language.types.boolean.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.