问题
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 thein_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