# dataxspy

New Members

1

0 Neutral

• Rank
Newbie
1. ## Calculating compound interest

I tried using a for loop but I'm trying to think of a way to calculate interest for a number of years but after I have the amount of interest for one year I'm unsure of how to add the interest to the principle then calculate it again for the next year, to hold that number like \$Principle + \$Interest = \$NewPrinciple then \$NewPrinciple + \$Interest for multiple years. <?php if (isset(\$_POST['submit'])) { \$InvestmentFrequency = \$_POST['InvestmentFrequency']; \$CompoundFrequency = \$_POST['CompoundFrequency']; \$InitialInvestment = preg_replace("/[^0-9]/", '', \$_POST['InitialInvestment']); \$Contributions = preg_replace("/[^0-9]/", '', \$_POST['Contributions']); \$InvestmentLength = preg_replace("/[^0-9]/", '', \$_POST['InvestmentLength']); \$InterestRate = \$_POST['InterestRate']; if (\$InvestmentFrequency == "Weekly") { \$InvestmentFrequency = "52"; } elseif (\$InvestmentFrequency == "Monthly") { \$InvestmentFrequency = "12"; } else { \$InvestmentFrequency = "1"; } if (\$CompoundFrequency == "Annually") { \$CompoundFrequency = "1"; } elseif (\$CompoundFrequency == "Semiannually") { \$CompoundFrequency = "2"; } elseif (\$CompoundFrequency == "Monthly") { \$CompoundFrequency = "12"; } elseif (\$CompoundFrequency == "Weekly") { \$CompoundFrequency = "52"; } else { \$CompoundFrequency = "365"; } if (\$CompoundFrequency == "1") { \$AdditionalInvestments = \$Contributions * \$InvestmentFrequency; \$Principle = \$InitialInvestment + \$AdditionalInvestments; \$Interest = \$Principle * \$InterestRate; \$NewPrinciple = \$Principle + \$Interest; for (\$x = 0; \$x <= \$InvestmentLength; \$x++){ echo "\$x - \$NewPrinciple</br>"; } } } This is the output I get the principle and interest added together for the number of years I entered.
×

• #### Activity

• Chat
×
• Create New...

## Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.