why does my function always return false?
i think the problem is caused by the isset function but i really dont know how to fix it
$big = array(
2,3,5,7,11,13,17,19,23
,29,31,37);
$fbig = array_flip ($big);
function isprime($n){
if($n < 2){
return FALSE;
}
if($n > 2147483647){
return FALSE;
}
if($n < 46341){
if(isset($fbig[$n])){
return TRUE;
} else {
return FALSE;
}
}
}
$b = 11;
if(isprime($b)){echo "lol";}
This line is the problem.
What you want to check is not
isset($fbig[$n])(which checks if there is something in the array at the index$n) butin_array($n, $fbig)(which checks if the array$fbigcontains the value$n).The array
$fbigis not in the scope of the function since it’s defined outside. But you can pass it:if(isprime($b, $fbig)){echo "lol";}should work just fine.