Heim > Artikel > Backend-Entwicklung > Amortisationstabelle auf Basis von PHP
Amortization Table
The Function of Recursive Function
What’s the Recursive Function?
Often, simply relying on a function to do something is insufficient; a script’s outcome might depend on a function’s outcome, or on changes in data resulting from its execution. Yet variable scoping prevents information from easily being passed from a function body back to its caller, so how can we accomplish this? You can pass data back to the caller by way of the return keyword.
<code><span><?php</span><span><span>function</span><span>amortizationTable</span><span>(<span>$paymentNum</span>,<span>$periodicPayment</span>,<span>$balance</span>,<span>$monthlyInterest</span>)</span> {</span><span>$paymentInterest</span>=round(<span>$balance</span>*<span>$monthlyInterest</span>,<span>2</span>); <span>$paymentPrincipal</span>=round(<span>$periodicPayment</span>-<span>$paymentInterest</span>,<span>2</span>); <span>$newBalance</span>=round(<span>$balance</span>-<span>$paymentPrincipal</span>,<span>2</span>); <span>print</span><span>" <tr> <td>$paymentNum</td> <td>\$"</span>.number_format(<span>$balance</span>,<span>2</span>).<span>"</td> <td>\$"</span>.number_format(<span>$periodicPayment</span>,<span>2</span>).<span>"</td> <td>\$"</span>.number_format(<span>$paymentInterest</span>,<span>2</span>).<span>"</td> <td>\$"</span>.number_format(<span>$paymentPrincipal</span>,<span>2</span>).<span>"</td> </tr>"</span>; <span>#If balance not yet zero ,recursively call amortizationTable()</span><span>if</span>(<span>$newBalance</span>><span>0</span>) { <span>$paymentNum</span>++; amortizationTable(<span>$paymentNum</span>,<span>$periodicPayment</span>,<span>$newBalance</span>,<span>$monthlyInterest</span>); } <span>else</span> { <span>exit</span>; } }<span>#end amortizationTable()</span><span>?></span><span><?php</span><span>#load balance</span><span>$balance</span>=<span>200000.0</span>; <span>#load interest rate</span><span>$interestRate</span>=<span>.0575</span>; <span>#monthly interest rate</span><span>$monthlyInterest</span>=<span>.0575</span>/<span>12</span>; <span>#Term length of the load, in years.</span><span>$termLength</span>=<span>30</span>; <span>#Number of payments per year.</span><span>$paymentsPerYear</span>=<span>12</span>; <span>#payment iteration</span><span>$paymentNumber</span>=<span>1</span>; <span>#Perform preliminary calculations</span><span>$totalPayments</span>=<span>$termLength</span>*<span>$paymentsPerYear</span>; <span>$intCal</span>=<span>1</span>+<span>$interestRate</span>/<span>$paymentsPerYear</span>; <span>$periodicPayment</span>=<span>$balance</span>*pow(<span>$intCal</span>,<span>$totalPayments</span>)*(<span>$intCal</span>-<span>1</span>)/(pow(<span>$intCal</span>,<span>$totalPayments</span>)-<span>1</span>); <span>$periodicPayment</span>=round(<span>$periodicPayment</span>,<span>2</span>); <span>#create table</span><span>echo</span><span>"<table width='50%' align ='center' border='1'>"</span>; <span>print</span><span>"<tr> <th>Payment Number</th><th>Balance</th> <th>Payment</th><th>Interest</th><th>Principal</th> </tr>"</span>; <span>#call recursive function</span> amortizationTable(<span>$paymentNumber</span>,<span>$periodicPayment</span>,<span>$balance</span>,<span>$monthlyInterest</span>); <span>#close table</span><span>print</span><span>"</table>"</span>;</code>
While I WAS compiling in PHPSTORM
Here is the result in my Safari Browser
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了Amortization Table base on PHP,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。