简体   繁体   English

PHP的in_array意外结果

[英]PHP's in_array unexpected result

When I execute this small piece of PHP code: 当我执行这一小段PHP代码时:

php -r "echo(in_array(0, array('aaa', 'bbb')));"

That echoes true ... 这回事真实 ......

Of course I have the good return value if I add the strict flag to in_array , but I just can't understand why it returns true (I can't !!). 当然,如果我将严格标志添加到in_array ,我有很好的返回值,但我只是无法理解为什么它返回true (我不能!!)。 If anyone can explain me I will sleep well tonight. 如果有人能解释我,今晚我会睡得好。

PS: Sorry that this is just about curiosity... PS:对不起,这只是好奇心......

That's because for PHP this code will return true 那是因为对于PHP这段代码将返回true

0 == 'aaa'

So without strict checking PHP will find your value in given array. 所以没有严格的检查, PHP会在给定的数组中找到你的值。

Also, check out this code: 另外,看看这段代码:

   var_dump(0 == 'aaa');
   var_dump(0 === 'aaa');
   var_dump(in_array(0, array('aaa', 'bbb')));
   var_dump(in_array(0, array('aaa', 'bbb'), true));

The last version allows strict type comparison so it will work as expected, that is return false. 最后一个版本允许严格的类型比较,因此它将按预期工作,即返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM