Skipjackrick Posted March 8, 2010 Share Posted March 8, 2010 Is it possible to do something like I have below? Basically I've got several conditions that need to be true for an if statement. <?php $condition1=1; $condition2=1; $condition3=1; $condition4=0; if ( $condition1 == 1 && $condition2 == 1 && $condition3 == 1 && $condition4 == 1) { //Update kayak man points $count_kmpoints = "UPDATE anglers SET kayakman_points = kayakman_points + 1 WHERE anglerId=$angler"; $counted_kmpoints = mysql_query($count_kmpoints) or exit(mysql_error()); } else { echo "Sorry, you haven't met the criteria to score any Kayak Man points"; } ?> Link to comment https://forums.phpfreaks.com/topic/194531-if-statement-syntax-for-multiple-conditions/ Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 Yep, that's exactly how you structure it. You can also separate them which can get messy: if($condition1 == 1){ if($condition2 == 1){ //etc etc } } Or you could place it into a function which would be more manageable but possibly unecessary in this instance: function isValid($conditions){ foreach($conditions as $condition){ if($condition != 1){ return false; } } return true; } // USAGE: $c['condition1'] = 1; $c['condition2'] = 1; $c['condition3'] = 1; $c['condition4'] = 0; if(isValid($c)){ // they are all valid } Basically, that means you can quickly run through a whole host of conditions without ever having to use anything other than the isValid() line. You could go a step further an add the ability to specify if any conditions can be false, or what certain conditions should return etc. etc. Link to comment https://forums.phpfreaks.com/topic/194531-if-statement-syntax-for-multiple-conditions/#findComment-1023128 Share on other sites More sharing options...
Skipjackrick Posted March 8, 2010 Author Share Posted March 8, 2010 Ah yeah!! Thanks! I like that foreach method..... seems a bit easier to work with many different combinations of conditions. Link to comment https://forums.phpfreaks.com/topic/194531-if-statement-syntax-for-multiple-conditions/#findComment-1023136 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.