Fog Juice Posted April 22, 2009 Share Posted April 22, 2009 Hi, I'm in a bit of a pickle. I have three variables, var1, var2, and var3. Each one is randomly assigned a number between 1 and 12. I want to write an if statement that will find out if any of those variables equal 2,3,or 4 all together. So in otherwords, I want to find out if all 3 variables fit one of the following 27 combination: 2-2-2 2-2-3 2-2-4 2-3-2 2-4-2 2-3-3 2-3-4 2-4-3 2-4-4 3-2-2 3-2-3 3-2-4 3-3-2 3-3-3 3-3-4 3-4-2 3-4-3 3-4-4 4-2-2 4-2-3 4-2-4 4-3-2 4-3-3 4-3-4 4-4-2 4-4-3 4-4-4 In words it goes something like "if (var1 is equal to 2, 3, or 4) and (var2 is equal to 2, 3, or 4) and (var3 is equal to 2, 3, or 4) then do this". Unfortunately I cannot do if(($var1 == 2 || 3 || 4) && ($var2 == 2 || 3 || 4) && ($var3 == 2 || 3 || 4)) because it doesn't give proper results. If anyone has any idea how I can do this without having to write 27 if/else if statements to check for each combination, that would be great. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/155148-if-statement-to-find-every-combination-of-2s3s4s-against-2s3s4s/ Share on other sites More sharing options...
Fog Juice Posted April 22, 2009 Author Share Posted April 22, 2009 If this helps anyone, try to think of it as a 3 reeled slot machine. There is BAR, double BAR, and TRIPLE bar. You win $5 when the reels display 3 x any BAR. So we need to figure out how the computer calculates the combination without doing 27 if statements. I'm doing this for a little slot machine php app and this has been driving me crazy. Quote Link to comment https://forums.phpfreaks.com/topic/155148-if-statement-to-find-every-combination-of-2s3s4s-against-2s3s4s/#findComment-816145 Share on other sites More sharing options...
markjoe Posted April 22, 2009 Share Posted April 22, 2009 Any single condition in the if must be a valid comparison by itself. looking at: if($var1 = 2 || 3 || 4) if(3) doesn't work if(($var1==2 || $var1==3 || $var1==4) && ($var2==2 || $var2==3 || $var2==4) && ($var3==2 || $var3==3 || $var3==4)) Remember that && and || are binary operators, hence they must operate on 2 (bi) values. Quote Link to comment https://forums.phpfreaks.com/topic/155148-if-statement-to-find-every-combination-of-2s3s4s-against-2s3s4s/#findComment-816161 Share on other sites More sharing options...
Fog Juice Posted April 22, 2009 Author Share Posted April 22, 2009 Any single condition in the if must be a valid comparison by itself. looking at: if($var1 = 2 || 3 || 4) if(3) doesn't work if(($var1==2 || $var1==3 || $var1==4) && ($var2==2 || $var2==3 || $var2==4) && ($var3==2 || $var3==3 || $var3==4)) Remember that && and || are binary operators, hence they must operate on 2 (bi) values. damn it so simple. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/155148-if-statement-to-find-every-combination-of-2s3s4s-against-2s3s4s/#findComment-816169 Share on other sites More sharing options...
sasa Posted April 22, 2009 Share Posted April 22, 2009 try <?php $test = array(2,3,4); $var1 = 2; $var2 = 2; $var3 = 3; if (count(array_diff(array($var1,$var2,$var3),$test)) == 0){ echo 'OK'; } else { echo 'NOT'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155148-if-statement-to-find-every-combination-of-2s3s4s-against-2s3s4s/#findComment-816170 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.