基於PHP的攤銷表
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 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.
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教程有兴趣的朋友有所帮助。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

許多用戶在選擇智慧型手錶的時候都會選擇的華為的品牌,其中華為GT3pro和GT4都是非常熱門的選擇,不少用戶都很好奇華為GT3pro和GT4有什麼區別,下面就給大家介紹一下二者。華為GT3pro和GT4有什麼差別一、外觀GT4:46mm和41mm,材質是玻璃鏡板+不鏽鋼機身+高分纖維後殼。 GT3pro:46.6mm和42.9mm,材質是藍寶石玻璃鏡+鈦金屬機身/陶瓷機身+陶瓷後殼二、健康GT4:採用最新的華為Truseen5.5+演算法,結果會更加的精準。 GT3pro:多了ECG心電圖和血管及安

機器之能報道編輯:吳昕國內版的人形機器人+大模型組隊,首次完成疊衣服這類複雜柔性材料的操作任務。隨著融合了OpenAI多模態大模型的Figure01揭開神秘面紗,國內同行的相關進展一直備受關注。就在昨天,國內"人形機器人第一股"優必選發布了人形機器人WalkerS深入融合百度文心大模型後的首個Demo,展示了一些有趣的新功能。現在,得到百度文心大模型能力加持的WalkerS是這個樣子的。和Figure01一樣,WalkerS沒有走動,而是站在桌子後面完成一系列任務。它可以聽從人類的命令,折疊衣物

為什麼截圖工具在Windows11上不起作用了解問題的根本原因有助於找到正確的解決方案。以下是截圖工具可能無法正常工作的主要原因:對焦助手已開啟:這可以防止截圖工具開啟。應用程式損壞:如果截圖工具在啟動時崩潰,則可能已損壞。過時的圖形驅動程式:不相容的驅動程式可能會幹擾截圖工具。來自其他應用程式的干擾:其他正在運行的應用程式可能與截圖工具衝突。憑證已過期:升級過程中的錯誤可能會導致此issu簡單的解決方案這些適合大多數用戶,不需要任何特殊的技術知識。 1.更新視窗與Microsoft應用程式商店應用程

第1部分:初始故障排除步驟檢查蘋果的系統狀態:在深入研究複雜的解決方案之前,讓我們先從基礎知識開始。問題可能不在於您的設備;蘋果的伺服器可能會關閉。造訪Apple的系統狀態頁面,查看AppStore是否正常運作。如果有問題,您所能做的就是等待Apple修復它。檢查您的網路連接:確保您擁有穩定的網路連接,因為「無法連接到AppStore」問題有時可歸因於連接不良。嘗試在Wi-Fi和行動數據之間切換或重置網路設定(「常規」>「重置」>「重置網路設定」>設定)。更新您的iOS版本:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

watch4pro和gt各自具有不使用的特點和適用場景,如果注重功能的全面性、高性能和時尚外觀,同時願意承擔較高的價格,那麼Watch 4 Pro可能更適合。如果對功能要求不高,更注重電池續航力和價格的合理性,那麼GT系列可能更適合。最終的選擇應根據個人需求、預算和喜好來決定,建議在購買前仔細考慮自己的需求,並參考各種產品的評測和比較,以做出更明智的選擇。

THE是什麼幣種?THE(TokenizedHealthcareEcosystem)是一種數位貨幣,利用區塊鏈技術,專注於醫療健康產業的創新和改革。 THE幣的使命是利用區塊鏈技術來提高醫療產業的效率和透明度,推動各方之間更有效率的合作,包括病患、醫護人員、製藥公司和醫療機構。 THE幣的價值和特徵首先,THE幣作為一種數位貨幣,具備了區塊鏈的優勢——去中心化、安全性高、交易透明等,讓參與者能夠信任和依賴這個系統。其次,THE幣的獨特之處在於它專注於醫療健康產業,並藉助區塊鏈技術改造了傳統醫療體系,提升了
