Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

greater than x and less than y php not working [closed]

I'm trying to declare a variable depending on what the number is.

I'm trying to do "if $var is less than [amount] and greater than [amount] var = blah elseif $var is less than [amount] and greater than [amount] var = blah" etc. Here is my code:

$randomNumber = rand(1, $funnyPicsHigh);

if($randomNumber > 0 && $randomNumber < 2001){
$directory_to_use = 1;

}elseif($randomNumber > 2000 && $randomNumber < 4001){
$directory_to_use = 2;

}elseif($randomNumber > 4000 && $randomNumber < 6001){
$directory_to_use = 3;

}elseif($randomNumber > 6000 && $randomNumber < 8001){
$directory_to_use = 4;

}elseif($randomNumber > 8000 && $randomNumber < 10001){
$directory_to_use = 5;

}elseif($randomNumber > 10000 && $randomNumber < 12001){
$directory_to_use = 6;

}elseif($randomNumber > 10000 && $randomNumber < 12001){
$directory_to_use = 7;

}elseif($randomNumber > 12000 && $randomNumber < 14001){
$directory_to_use = 8;

}elseif($randomNumber > 14000 && $randomNumber < 16001){
$directory_to_use = 9;

}elseif($randomNumber > 16000 && $randomNumber < 18001){
$directory_to_use = 10;

}elseif($randomNumber > 18000 && $randomNumber < 20001){
$directory_to_use = 11;

}

I started out using if then elseif for everything, I then changed it so it is >= and <=, still didn't work, then I tried putting " " before and after my numbers which is what you see now, still didn't work.

The random number executes just fine, but it seems to pick a random directory. I'll refresh the page and it picks 5 for the directory to use. I refresh it again and it picks 1, then picks 1, then picks 3. It seems to just randomly pick a number.

What am I doing wrong?


FIGURED it out, thank you everyone. It was due to my logic not the code provided. Talking out loud trying to explain my problem is what helped me so thanks. If wondering what the working code is, it's here: http://pastebin.com/en2sc3HC

like image 996
Travis Avatar asked Dec 06 '22 16:12

Travis


1 Answers

Your code can be significantly simplified

$directory_to_use = ceil(rand(1, $funnyPicsHigh) / 2000);

if that is a typo

}elseif($randomNumber > 10000 && $randomNumber < 12001){
$directory_to_use = 6;

}elseif($randomNumber > 10000 && $randomNumber < 12001){
$directory_to_use = 7;
like image 83
Cheery Avatar answered Dec 10 '22 11:12

Cheery