PHP IN_ARRAY 函数使用注意事项_PHP

WBOY
Release: 2016-06-01 12:18:01
Original
913 people have browsed it

另外,我们在 控制结构比较两个数值是否一直的时候,也应该尽量使用 === 来代替 ==(当然,这个也根据具体的业务逻辑选用比较合适的)。

小弟来解释下 为什么
var_dump(in_array(0, array('s' ));

这句话的结果是bool(true)。

因为in_array会将0 和's' 进行比较,0是number类型,'s'是string类型,根据php manual 中“Comparison Operators” 一章的说明可知,number 和string进行

比较的时候,会先将string类型首先转化为number,然后再进行比较操作。 's'转化为number的结果为0,而0 == 0 的结果是true,所以in_array(0, array('s', 'ss'))的结果也是true

如果把in_array 的第三个参数strict设置为 true,比较的时候 就会判断值和类型是否都相当。如果都相当的话,才会返回true,否则返回false.

仅此为记。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!