关于in_array一个问题 [英] A problem about in_array

查看:154
本文介绍了关于in_array一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于 in_array 最近我无法理解一个奇怪的问题。
例如。

I have got a strange problem about in_array recently which I cannot understand. e.g.

$a = array('a','b','c');
$b = array(1,2,3);

if (in_array(0,$a))
{
    echo "a bingo!\n";
}
else
{
    echo "a miss!\n";
}

if (in_array(0,$b))
{
    echo "b bingo!\n";
}
else
{
    echo "b miss!\n";
}

我跑在我的灯,并得到了

I ran it on my lamp,and got

a bingo!
b miss!

我阅读手册,并设置第三个参数 $严格真正,然后担任expected.But这是否意味着我总是需要使用时, in_array 来设置严格的参数为真?建议将AP preciated。

I read the manual and set the third parameter $strict as true,then it worked as expected.But does that mean I always need to set the strict parameter as true when using in_array?Suggestions would be appreciated.

问候

推荐答案

这意味着你必须设置第三个参数真正当你想比较为不仅比较值,而且类型

It means you have to set the third parameter to true when you want the comparison to not only compare values, but also types.

否则,有类型转换,一边做比较 - 看的字符串转换为数字,例如。

Else, there is type conversions, while doing the comparisons -- see String conversion to numbers, for instance.

由于事实上, in_array 并没有严格的就是因为你有之间的差别一样== === - 看到的比较运算符

As a matter of fact, in_array without and with strict is just the same difference as you'll have between == and === -- see Comparison Operators.

结果
这种转换,大部分的时间,工程确定......但不是在的情况下你试图与以字母开头的字符串比较0:字符串被转换为数字,其中有0作为值


This conversion, most of the time, works OK... But not in the case you're trying to compare 0 with a string that starts with a letter : the string gets converted to a numeric, which has 0 as value.

这篇关于关于in_array一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆