...

View Full Version : elseif to switch



Bob42
02-24-2008, 04:42 PM
I have a script with a bunch of elseif statements. This would probably work best with a switch statement.



if (isset($_POST['get_random'])) {
$number = $_POST['random'];

if ($number <= 10) {
$generate = rand(10,80);
echo $generate;
}
elseif ($number <= 20 && $number > 10) {
$generate = rand(20,200);
echo $generate;
}
elseif ($number <= 30 && $number > 20) {
$generate = rand(100,800);
echo $generate;
}
elseif ($number <= 40 && $number > 30) {
$generate = rand(750,1800);
echo $generate;
}
elseif ($number <= 50 && $number > 40) {
$generate = rand(700,3000);
echo $generate;
}
elseif ($number <= 60 && $number > 50) {
$generate = rand(700,4500);
echo $generate;
}
elseif ($number <= 70 && $number > 60) {
$generate = rand(1000,5000);
echo $generate;
}
elseif ($number <= 80 && $number > 70) {
$generate = rand(1100,7000);
echo $generate;
}
elseif ($number <= 90 && $number > 80) {
$generate = rand(1500,9000);
echo $generate;
}
elseif ($number <= 100 && $number > 90) {
$generate = rand(2000,10000);
echo $generate;
}
elseif ($number > 100) {
echo "Illegal level";
}
}




switch ($number) {
case 0:
$generate = rand(10,80);
echo $generate;
break;
}


That's what I have so far. I just don't know how to write up the comparison in the if/elseif statements.

matak
02-24-2008, 06:43 PM
i'm a bit tired but try this


if (isset($_POST['get_random'])) {
$number = $_POST['random'];
switch ($number) {
case 0:
$generate = rand(10,80);
echo $generate;
break;
}
}
elseif ($number > 100) {
echo "Illegal level";
}

oesxyl
02-24-2008, 11:11 PM
if (isset($_POST['get_random'])) {
$number = $_POST['random'];

$minlim = array(0 => 10,
20 => 20,
30 => 100,
40 => 750,
50 => 700,
60 => 700,
70 => 1000,
80 => 1100,
90 => 1500,
100 => 2000);

$maxlim = array(0 => 80,
20 => 200,
30 => 800,
40 => 1800,
50 => 3000,
60 => 4500,
70 => 5000,
80 => 7000,
90 => 9000,
100 => 10000);

if ($number > 100) {
echo "Illegal level";
}else{
$number -= $number % 10;
$generate = rand($minlim($number), $maxlim($number));
}
}


not tested, you must also check the values from $minlim and $maxlim

best regards

Bob42
02-25-2008, 11:34 PM
oesxyl:

I get an error when I enter in a number from the form:
Fatal error: Function name must be a string in C:\xampp\htdocs\5\rpgpointgen.php on line 38

Line 38 would be

$generate = rand($minlim($number), $maxlim($number));

Any idea what the error means?

oesxyl
02-26-2008, 12:02 AM
oesxyl:

I get an error when I enter in a number from the form:
Fatal error: Function name must be a string in C:\xampp\htdocs\5\rpgpointgen.php on line 38

Line 38 would be

$generate = rand($minlim($number), $maxlim($number));

Any idea what the error means?

$minlim and $maxlim are array, you must use
'[' and ']' not '(' and ')'.


$generate = rand($minlim[$number], $maxlim[$number]);

Is my fault, I write '(', ')' in my code.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum