Reputation:
Let's say $total=1000 and I need to subtract $a=100, $b=50, $c=10, $d=50 from $total and show the remainder each time. I would like to see:
$total-$a=$new_total //900
$new_total-$b=$new_total //850
$new_total-$c=$new_total //840
$new_total-$d=$new_total //790
The first line is easy but after that I'm lost. The subtracted numbers are comming from a form so I don't know how many there will be. Could be $a-$t or just $a. Hope this makes sense!
Upvotes: 1
Views: 297
Reputation: 24405
Swap your assignment around and echo the result each time:
echo PHP_EOL, $total = 1000;
echo PHP_EOL, $new_total = $total-$a; //900
echo PHP_EOL, $new_total = $new_total-$b; //850
echo PHP_EOL, $new_total = $new_total-$c; //840
echo PHP_EOL, $new_total = $new_total-$d; //790
Sounds like the number of these variables is dynamic. The easiest way to implement something like this with a variable number of inputs is to use an array in the frontend and loop it in the back:
<?php for ($i = 1; $i <= 10; $i++) : ?>
<input type="text" name="numbers[]" />
<?php endif; ?>
Then:
echo PHP_EOL, $total = 1000;
foreach ($_POST['numbers'] as $number) {
echo PHP_EOL, $total -= $number;
}
Upvotes: 1