codenoob codenoob - 5 months ago 6
PHP Question

php How to check if an array of numbers are all positive

I put 3 user inputs into an array. all numbers. and I want to check if all 3 numbers are positive.

The code below will return true when 1 of them is positive.

foreach($array as $value){
if($value >= 0){
return TRUE;
}else{
return FALSE;
}
}


Using heybignick 's suggestion. the below code seems to give me the desired result.

$array = array(1,2,3,4,-5);


if(min($array)<0){
echo "negative";
}else{
echo "positive";
}

Answer

Could you just use min to get the desired result?

if(min($array)<0) return FALSE;
return TRUE;
Comments