symbol and decimal number true in php

这一生的挚爱 提交于 2019-11-29 18:05:33

From the documentation for in_array():

If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.

In PHP, casting any string that doesn't begin with a number evaluates to to 0. The 0 exists in your array, so in_array() returns true. If you don't want this to happen, set the third parameter for in_array() to true, so it performs a strong comparison (equivalent to ===) and consider the types, too.

if (in_array('@', $number, true) === true) {
    echo "true";
}
else { 
    echo "false";
}

Output:

false
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!