Table of Contents
Returning Values from a Function
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:
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.
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.
Home Backend Development PHP Tutorial Amortization Table base on PHP

Amortization Table base on PHP

Aug 08, 2016 am 09:20 AM
Balance gt lt the

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>
Copy after login

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教程有兴趣的朋友有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

After 2 months, the humanoid robot Walker S can fold clothes After 2 months, the humanoid robot Walker S can fold clothes Apr 03, 2024 am 08:01 AM

Editor of Machine Power Report: Wu Xin The domestic version of the humanoid robot + large model team completed the operation task of complex flexible materials such as folding clothes for the first time. With the unveiling of Figure01, which integrates OpenAI's multi-modal large model, the related progress of domestic peers has been attracting attention. Just yesterday, UBTECH, China's "number one humanoid robot stock", released the first demo of the humanoid robot WalkerS that is deeply integrated with Baidu Wenxin's large model, showing some interesting new features. Now, WalkerS, blessed by Baidu Wenxin’s large model capabilities, looks like this. Like Figure01, WalkerS does not move around, but stands behind a desk to complete a series of tasks. It can follow human commands and fold clothes

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

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

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

What currency is THE? Is THE coin worth investing in? What currency is THE? Is THE coin worth investing in? Feb 21, 2024 pm 03:49 PM

What currency is THE? THE (Tokenized Healthcare Ecosystem) is a digital currency that uses blockchain technology to focus on innovation and reform in the healthcare industry. THE coin's mission is to use blockchain technology to improve the efficiency and transparency of the medical industry and promote more efficient cooperation among all parties, including patients, medical staff, pharmaceutical companies and medical institutions. The Value and Characteristics of THE Coin First of all, THE Coin, as a digital currency, has the advantages of blockchain - decentralization, high security, transparent transactions, etc., allowing participants to trust and rely on this system. Secondly, the uniqueness of THE coin is that it focuses on the medical and health industry, using blockchain technology to transform the traditional medical system and improve

See all articles