onocentaur Posted September 3, 2007 Share Posted September 3, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/67812-solved-if-statement-with-unknown-number-of-cases/ Share on other sites More sharing options...
AndyB Posted September 3, 2007 Share Posted September 3, 2007 Here's a start: $nums = explode(",", $yourcsvlist); // $nums is an array $howmany = count($nums); // total elements Quote Link to comment https://forums.phpfreaks.com/topic/67812-solved-if-statement-with-unknown-number-of-cases/#findComment-340793 Share on other sites More sharing options...
corbin Posted September 3, 2007 Share Posted September 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67812-solved-if-statement-with-unknown-number-of-cases/#findComment-340816 Share on other sites More sharing options...
onocentaur Posted September 4, 2007 Author Share Posted September 4, 2007 well, that looks like a compact piece of code that will do the job nicely. thankyou for your help! Quote Link to comment https://forums.phpfreaks.com/topic/67812-solved-if-statement-with-unknown-number-of-cases/#findComment-341132 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.