codenoob codenoob - 1 year ago 86
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;
return FALSE;

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

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

echo "negative";
echo "positive";

Answer Source

Could you just use min to get the desired result?

if(min($array)<0) return FALSE;
return TRUE;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download