Reason93
Reason93

Reputation: 11

New To PHP = Investment Calculator feedback

I'm new to and I'm wondering if I set this up right, for a question I have to do. I have to set up a loop that does $amount * (1 + $interest / 100) to find the number of years up to $1000000. The initial amount is $10000, but I'm not quite sure if I set up my if statement correctly.

<?php //Script investment.php

    //Address error handling.
    ini_set('display_errors',1);
    error_reporting(E_ALL & ~E_NOTICE);

    // In case register_globals is disabled.
    $return = 10000*(1+(15/100));
    $total = 1000000;    

    if ($year=$total/$return/12){  
        echo "number of years is : ".$year; 
    }  
?> 

Upvotes: 1

Views: 418

Answers (2)

Nidhi
Nidhi

Reputation: 1539

You can use this ,

$total = 1000000;
$year  = 0;
$amount = 10000;
$intrest = 15;

do {
    $int_amount = $amount*($intrest/100);
    $amount += $int_amount;
    $year++;
    echo "Year ".$year.", Amount = ".round($amount,2).", Intrest = ".round($int_amount,2)."<br/>";
} while ($amount <= $total);

echo "number of years is : ".$year;

Upvotes: 1

Matt
Matt

Reputation: 5428

I don't know the exact formula, but I think you're looking for something like this (Probably needs some tweaking):

<?php

$investment = 10000;
$interest = 15;
$totalInvestmentDesired = 100000;
$year = 0;


while(true) {
    if($investment >= $totalInvestmentDesired) break;
    $year++;
    $investment = $investment*(1+$interest/100);
    echo "Year: " . $year . " Investment: " . round($investment,2) . "<br>\n";
}

echo "\n<br>Investment: " . round($investment, 2). " Number of years: ".$year; 

Yields:

Year: 1 Investment: 11500
Year: 2 Investment: 13225
Year: 3 Investment: 15208.75
Year: 4 Investment: 17490.06
Year: 5 Investment: 20113.57
Year: 6 Investment: 23130.61
Year: 7 Investment: 26600.2
Year: 8 Investment: 30590.23
Year: 9 Investment: 35178.76
Year: 10 Investment: 40455.58
Year: 11 Investment: 46523.91
Year: 12 Investment: 53502.5
Year: 13 Investment: 61527.88
Year: 14 Investment: 70757.06
Year: 15 Investment: 81370.62
Year: 16 Investment: 93576.21
Year: 17 Investment: 107612.64


Investment: 107612.64 Number of years: 17

http://sandbox.onlinephpfunctions.com/code/7cf9f868ed86a9fc24e3ae76dcf73896675d7261

Upvotes: 1

Related Questions