symbol and decimal number true in php

╄→гoц情女王★ 提交于 2019-12-29 09:56:33

问题


I have script like this

$number = range(0, 9);

when I have condition like this

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

}else "false";

and output:

true

and my question is why the symbols is the same whit any number in array $number?? I want symbols just symbols not number.

example I want like this

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

}else "false";

output :

false

回答1:


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


来源:https://stackoverflow.com/questions/22917618/symbol-and-decimal-number-true-in-php

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