Jump to content

[SOLVED] If statement with unknown number of cases


onocentaur

Recommended Posts

I need to break out of a loop if an unknown number of conditions are satisfied.

 

I'm starting with a comma-delineated list of numbers of an unknown length (more than 1). I've got a function (we'll call it what($n) here) that'll return either true or false depending on $n. Now, I want to leave a loop (using continue) if it's not true that all the numbers in the list are true. Eh?

 

well, suppose the list is 1,2,5

then the code would be

if (!((what(1)) && (what(2)) && (what(5)))) continue;

 

The problem is, I don't know how long the list is, so I can't code a definite number of &&s! How can I get round this? I'm sure there must be a way but I can't think how!

(I guess it's probably fairly easy to convert my list into an array, I'm not sure how, but I guess it would be a good way to start)

 

Thanks!

I'm confused by your question (not because of how it was worded, but just because it's confusing ;p), but I think this is what you mean:

 

 

$nums = '1,2,3,4,5';
$num_arr = explode(',', $nums);
$fail = false;
foreach($num_arr as $v) {
     if(!what($v)) {
            $fail = true;
            break;
      }
}

if($fail === true) {
     echo 'it didn\'t work!';
}
else {
     echo 'All numbers returned true in the function what.';
}

 

There's probably a more effecient way to do that, possibly involving array_map, but I'm too lazy to look into it ;p.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.