• Notice regarding image hosts and attachments. See here.

Setting Variable Parameter with IF PHP

Associate
Joined
2 Jan 2007
Posts
427
Location
Epsom, Surrey
Hey all,

just wondering if its possible to set a variable using an IF Statement:



$baseprice = 15;

if ($_POST['slots']=="10"){
$slotsum = 0;
} elseif ($_POST['slots']=="12"){
$slotsum = 1;
} elseif ($_POST['slots']=="14"){
$slotsum = 2;
} elseif ($_POST['slots']=="16"){
$slotsum = 3;
} elseif ($_POST['slots']=="18"){
$slotsum = 4;
} elseif ($_POST['slots']=="20"){
$slotsum = 5;
} elseif ($_POST['slots']=="22"){
$slotsum = 6;
} elseif ($_POST['slots']=="24"){
$slotsum = 7;
} elseif ($_POST['slots']=="26"){
$slotsum = 8;
} elseif ($_POST['slots']=="28"){
$slotsum = 9;
} elseif ($_POST['slots']=="30"){
$slotsum = 10;
}else {
$slotsum = 11;
}


$total=$baseprice + $slotsum;



Could this work?

Thanks
 
Yeah, i have tested it but im only getting it read out 15.

Its not adding the slotsum variable.


Ive changed it slightly to:

$baseprice = 15;


function slotSum(){

if ($gameslots=="10"){
$slotsum = 0;
} elseif ($gameslots=="12"){
$slotsum = 1;
} elseif ($gameslots=="14"){
$slotsum = 2;
} elseif ($gameslots=="16"){
$slotsum = 3;
} elseif ($gameslots=="18"){
$slotsum = 4;
} elseif ($gameslots=="20"){
$slotsum = 5;
} elseif ($gameslots=="22"){
$slotsum = 6;
} elseif ($gameslots=="24"){
$slotsum = 7;
} elseif ($gameslots=="26"){
$slotsum = 8;
} elseif ($gameslots=="28"){
$slotsum = 9;
} elseif ($gameslots=="30"){
$slotsum = 10;
}else {
$slotsum = 11;
}

}



$total=$baseprice + slotSum();;



Still only getting the $baseprice.
 
Holycarp, why are you using that insane if statement? $slotsum = 10 + ($gameslots - 10) / 2!

Let alone that insane function, which won't work because the variables are held within the function.

But yes, you can set a variable within pretty much whatever you like, otherwise the language would be useless :)
 
S'alright, that's why this place exists :)

Remove the function, basically, and it'll work (or should - that'll solve the variable scope problem.)

Always best to get something working then tidy up code after:)
 
Back
Top Bottom