Here is the PHP im working with:
Below values from a database:
$quote = 49;
$add_amp = 0;
$con_array = "3,2,1";
function calcAddAmp($mul)
{
$add_amp = $add_amp + $mul*($quote);
}
$con_array = explode(",", $con);
for($i = 0; $i < count($con_array); $i++) {
switch ($fun) {
case 1:
calcAddAmp(.01);
break;
case 2:
calcAddAmp(.05);
break;
case 3:
calcAddAmp(.02);
break;
case 4:
calcAddAmp(.09);
break;
}
}
$quote = $quote + $add_amp;
The issue is seen here:
echo $add_amp;
$add_amp returns 0, which makes $quote the same value as before, 49.
echo $quote;
I’m sure I am just writing this function incorrectly, but I can’t find where I went wrong.
Variables have function scope. Setting
$add_ampinside a function does not automatically modify the variable by the same name outside the function. If you don’treturnthe value from the function it’s pointless. I’d simplify this whole unnecessaryswitch/functionto this though: