I have a bunch a values I would like to add together which are entered into a form. Right now, the form has 11 lines but it could get larger in the future. I can easily add all the values together with something like:
$total = $value1 + $value2 + ... + $value11;
All the values I want to add together are coming from an HTML form. I want to avoid javascript.
But, I want to avoid having to manually do it, especially if it grows much larger. This is my attempt at adding all the values together using a loop but it returns an ‘undefined variable’ error (it is just some test code to try out the idea):
<?php $tempTotal = 0; $pBalance1 = 5; $pBalance2 = 5; $pBalance3 = 5; for ($i = 1 ; $i <= 3 ; $i++){ $tempTotal = $tempTotal + $pBalance.$i; } echo $tempTotal; ?>
Is what I want to do possible in PHP?
This will do what you want. However you might indeed consider using an array for this kind of thing.