I want to do a certain feature on about 3% of the sites visitors, is the best way to do this as follows? $number = rand(1,100); if($number == 1 || 2 || 3) { //Do this for 3% } PHP: If not how can I do this? If there's an easier way in php or javascript then I'd like to know, Thanks
Actually if ($number <= 3 && $number >= 1) { // DO X } This would check that the number is 3 or smaller, but 1 or bigger Anyway, the problem you're having is here: if($number == 1 || 2 || 3) you can't do it like that, it will check if 2 or 3 are true, which they are and always "do x" what you would need to do for correct coding using your method would be: if($number == 1 || $number == 2 || $number == 3)
I'd probably do this so you only have to check one equals condition. if (rand(0,33)==0) { I know it's not exactly 3%, but you did say 'about 3%'. Should be close enough since it didn't sound like you needed accuracy. One thing for certain, I wouldn't be wasting a variable declaration on it. If you needed accurate, I'd just use: if (rand(0,99)<3) { Since he's doing rand(1,100), his value would NEVER be less than 1, so there's no reason to check for that!