I need to calculate the sum of all loop result. Please my code below:

I am currently computing the deposit which is 100 plus interest rate and adds 100 in every year within a loop. That goal has been achieved but I need help in outputting the total of the iterated results from this loop. Please example code below. Thank you!

  <?php
            $deposit = 100;
            $rate = 5;
            $rate = $rate / 100;
            $year = 0;

            while($year < 5){
                $year++;
                $deposit = $deposit + ($deposit * $rate);

                echo "<strong>Year " .$year . ": </strong>" . number_format($deposit,2) ."<br>";
                $total = $deposit = $deposit + 100;

                $total = $total + $year;
            }

            echo "-------------------------" . "<br><strong> Total: " . number_format($total,2);
            ?>

I get this result from the loop which is a good thing. My goal is to add all these results from year1 to year5.

THIS THE OUTPUT OF THE LOOP:

Year 1: 105.00
Year 2: 215.25
Year 3: 331.01
Year 4: 452.56
Year 5: 580.19

Total: 0 ----- This is my goal :slight_smile:

But what is the purpose of this?

My purpose to get the total after getting the result from the loop.

$total = $deposit = $deposit + 100; ----- This is fine. First year deposit is 100 + rate which is (0.05 of 100) from second year it add the previous computation plus another 100 because I want to add 100 in every year. This loop works fine. My only goal now is to get the total of all iteration from year 1 and so on.

$total = $total + $year; --------------------- Please discard this line of code.

according to the link, a total is given

Yes it was given because of my wrong code which the line that I wanted to discard. (I was experimenting to get the total.) The given total is wrong as you can see.

Were you expecting something more along these lines?

Year 1: 105.00
Year 2: 210.00
Year 3: 315.00
Year 4: 420.00
Year 5: 525.00


Total: 525.00

Or should there be compound interest?

Year 1: 105.00
Year 2: 215.25
Year 3: 331.01
Year 4: 452.56
Year 5: 580.19

The exact total should be “1,684.01”. I just used my windows calculator lol. So, this is my goal now after looping is achieved. I just could not figure out this area of getting the total.

I already did the compounding interest in the loop. So, no prob with that.

Trying to understand where you got your total figure from.

It’s by adding year 1 value up to year 5 value. And the result is 1,684.01 using windows calc.
Year 1: 105.00
Year 2: 215.25
Year 3: 331.01
Year 4: 452.56
Year 5: 580.19

var_dump(array_sum([105, 215.25, 331.01, etc]));

What I mean is, it’s already a running tally. If you put $100 in the bank and $100 annually, you have $500 total principle balance. What I am trying to understand is why you think you are going to make $1184 in interest in 5 years, at a 5% interest rate. This is the breakdown over 5 years

Year principle interest total
1 100 5 105
2 200 15.25 215.25
3 300 31.01 331.01
4 400 52.56 452.56
5 500 80.19 580.19

At the end of those 5 years, you have 580.19 not 1684.

Sorry my bad. This is because it’s my first week of learning programming using php as my first language of coding. Sorry about that and thank you for your effort and help.

How do you echo the last value of Year 5 as the total? I wanted to output it below the Year 5 as the total savings. :slight_smile: Thank you so much!

Think about how to do it and test some ideas out

1 Like

I already did which is to minus 100 from total and echoed outside the while loop and it worked but when doing so with user input using basic form submission ---- it deducts to itself and the result is zero. :frowning: I tried my best.

What user input are you actually taking in, that makes a difference.

In my original code. I set the deposit 100 as constant but when using form input it depends on the user input. My goal now is to echo the total outside the loop which is 580. It gives me an error (undefined variable).

<link rel="stylesheet" type="text/css" href="main.css">
<center>
<form action="" method="post">
<input type="number" placeholder="Initial Deposit" name="deposit" style="width:130px;" min="1" max="1000000"><br><br>
<input type="number" placeholder="Annual Deposit" name="addon" style="width:130px;" min="1" max="1000000"><br><br>
<input type="number" placeholder="Number of Years"name="year" style="width:130px;" min="1" max="1000000"><br><br>
<input type="number" name="rate" style="width:40px;" min="1" max="12" value="" /> <strong>% &nbsp;<br><br>Annual Interest Rate</strong>
<br><br><br>

<input type="submit" name="submit" style="width:130px; height:35px;">
</form>
</center>



<?php
$counter = 0;
$year = 0;
//$deposit2 = 0;
$add_on = 0;
$to_minus = 0;

//Form Validation
if(isset($_POST['submit']))
{
	if(empty($_POST['rate']) || empty($_POST['deposit']) || empty($_POST['addon']) || empty($_POST['year']))
		{
			echo "<div class=report2> Missing some information. Please complete the form!</div>"; 
		}
	else
		{
			$rate = $_POST['rate'];
			$deposit = $_POST['deposit'];	
			$add_on = $_POST['addon'];
			$counter = $_POST['year'];
			//$to_minus = $_POST['addon'];
			$total2 = $_POST['deposit'];

			echo "<div class=report>Below is your " . "<strong>" . $counter . "-year Savings Forecast Report</strong><br>________________________________</div>		" ."<br><br>";
}

//Calculations

// $deposit = 0;

while($year < $counter)
	{
		$year++;
		$deposit = $deposit + ($deposit * $rate/100);
		//$deposit2 += $deposit;

		echo "<div class=report2><strong>Year " .$year . ": </strong>" .  number_format($deposit,2). "<br>" . "</div>";
		$total = $deposit = $deposit + $add_on;


	}
}



?>

So you have variables where you don’t need them and missing others you do.

Not how I would prefer to do it, but follow the logic and ask questions on what you don’t understand.

I finally solved it. Thank you so much!

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service