• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Amortization Table base on PHP

php 搞代码 3年前 (2022-01-23) 23次浏览 已收录 0个评论

Amortization Table

The Function of Recursive Function

What’s the Recursive Function?

Returning Values from a 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 e(、本文来源gao@!dai!ma.com搞$$代^@码网*搞gaodaima代码xecution. 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.

Recursive functions, or functions that call themselves, offer considerable practical value to the programmer and are used to divide an otherwise complex problem into a simple case, reiterating that case until the problem is resolved.

Practically every introductory recursion example involves factorial computation. Yawn. Let’s do something a tad more practical and create a loan payment calculator. Specifically, the following example uses recursion to create a payment schedule, telling you the principal and interest amounts required of each payment installment to repay the loan. The recursive function, amortizationTable(),It takes as input four arguments:

paymentNum, which identifies the payment number, periodicPayment, which carries the total monthly payment, balance, which indicates the remaining loan balance, and monthlyInterest, which determines the monthly interest percentage rate. These items are designated or deter- mined in the script listed below here:

<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

Shows sample output, based on monthly payments made on a 30-year fixed loan of $200,000.00 at 6.25 percent interest. For reasons of space conservation, just the first 10 payment iterations are listed.

Here is the result in my Safari Browser

Employing a recursive strategy often results in significant code savings and promotes reusability. Although recursive functions are not always the optimal solution, they are often a welcome addition to any language’s repertoire.

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了Amortization Table base on PHP,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Amortization Table base on PHP

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址