1

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?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Haabda
  • 1,413
  • 2
  • 13
  • 17

9 Answers9

7
for  ($i = 1 ; $i <= 3 ; $i++){
    $varName = "pBalance".$i;
    $tempTotal += $$varName;
}

This will do what you want. However you might indeed consider using an array for this kind of thing.

Lasar
  • 5,175
  • 4
  • 24
  • 22
7

I would use @unexist's solution.. give the input fields names like:

<input name="myInput[]" />
<input name="myInput[]" />
<input name="myInput[]" />
...

Then in your PHP get the sum like this:

$total = array_sum($_REQUEST['myInput']);

Because of the '[]' at the end of each input name, PHP will make $_REQUEST['myInput'] automatically be an array. A handy feature of PHP!

Keeth
  • 2,100
  • 2
  • 21
  • 29
3

Uhm why don't you use an array? If you give the forms a name like foobar[] it will be an array in PHP.

unexist
  • 2,518
  • 23
  • 27
  • That what I am speaking of. When the name of every field in the form has the same name that ends with [] it will be an array in $_POST. – unexist Sep 24 '08 at 22:11
  • I never knew that! Thank you. That could come in very useful. – Haabda Sep 24 '08 at 22:12
2

The values you need are posted from a form, right? If so, you could iterate through the keys in the $_POST variable that match your form field's generated names.

foreach($_POST as $key=>$value)
{
  if(strpos($key, 'pBalance')===0)
  {
    $final_total += $value;
  }
}
Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
2

You can use an array to store your data, and just loop over it.

$tempTotal = 0;

$balances[] = 5;
$balances[] = 5;
$balances[] = 5;

for  ($i = 0; $i <= count($balances); $i++) {
    $tempTotal = $tempTotal + $balances[$i];
}

Or for brevity, use a foreach loop:

foreach($balances as $balance) {
    $tempTotal += $balance;
}
conmulligan
  • 7,038
  • 6
  • 33
  • 44
2

If you're trying to collect the values of a POST, you should really use an array. You can avoid having to manually piece together such an array by using:

<input type="text" name="vals[]" value="one" />
<input type="text" name="vals[]" value="two" />

$_POST["vals"] will then be array("one", "two");

William Keller
  • 5,256
  • 1
  • 25
  • 22
1

In about 99% of all cases involving a PHP generated variable, you are doing The Wrong Thing. I'll just re-iterate what others have said:

USE AN ARRAY

Jonathan Arkell
  • 10,526
  • 2
  • 24
  • 32
0

Try

$varName = 'pBalance' . $i;
$tempTotal = $tempTotal + $$varName;
Doug T.
  • 64,223
  • 27
  • 138
  • 202
-1

The concept you're looking for is called a variable variable (at least it's called that in PHP). Here is the official documentation and a useful tutorial. The syntax for a variable variable is a double dollar sign ($$).

VirtuosiMedia
  • 52,016
  • 21
  • 93
  • 140