Jump to content

Modulus


Ninjakreborn

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/250637-modulus/#findComment-1285949
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/250637-modulus/#findComment-1285953
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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