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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.