Ninjakreborn Posted November 7, 2011 Share Posted November 7, 2011 How can I write: if ($num == 1 || $num == 6 || $num == 11 || $num == 16 || $num == 21) { To use Modulus operator instead. Trying to figure out more about how to use it, so this is a good situation. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 7, 2011 Share Posted November 7, 2011 Well, if those are the ONLY values you want to check, then what you have will suffice. You could use modulus to check for each 5th instance, but then you need to add minimum and maximum validations. So the number of validations is not that different if($num>=1 && $num<=21 && $num%5==1) { Even though that only requires 3 checks as opposed to your original of 5, I would prefer your original code for the sake of readability. I can easily see what numbers the validation allows looking at your original code, whereas for the one using modulus it take some mental calculations. But, if you have a much wider minimum/maximum values then modulus would make sense. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 7, 2011 Author Share Posted November 7, 2011 Yes, the ultimate value I have set it at now is 201. I might even expand that to closer to 500. This works out really well though. If it was just a few numbers and ONLY that I would leave it, but I had an old mentor of mine once taught me about Modulus in a similar situation..so I started thinking of him again and was trying to remember how to do it. That helped a lot, thanks again man. Quote Link to comment 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.