如果针0 in_array返回true [英] in_array returns true if needle is 0

查看:106
本文介绍了如果针0 in_array返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 in_array 功能的问题。下面返回测试真正

I have an issue with in_array function. Test below returns true:

in_array(0, array('card', 'cash'))

这怎么可能,我怎么能prevent呢?

How is it impossible, how can I prevent it ?

然而

in_array(null, array('card', 'cash'))

收益

推荐答案

铸件不与PHP一个数字,一个数字结果 0 启动任何字符串。而这也正是 0 比较一些字符串时会发生什么。请参阅 PHP文档有关如何不同类型之间的比较完成的细节。

Casting any string that doesn't start with a digit to a number results in 0 in PHP. And this is exactly what happens when comparing 0 with some string. See the PHP docs for details about how comparisons between various types are done.

使用第三个参数(将其设置为真正)的 in_array 避免松散型的比较。

Use the third argument (set it to true) of in_array to avoid loose type comparison.

in_array(0, array('card', 'cash'), true) === false

这篇关于如果针0 in_array返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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