Car Loan Calculator – Php Basic Programming
Table of Contents
ToggleFirst we will have to create a new PHP file: simplecarloancalculator.php. A PHP file is treated by the web server as a normal HTML file except for the code written inside a php tag.
We start off by creating the car loan calculator HTML form submitting data back to this web page.
Car price: ___ Term: ___ Interest rate: ___ [Calculate]
Can be translated to:
When the calculate button is pressed the data in the text boxes will be sent to the page named: simplecarloancalculator.php (the page we have all ready have loaded in our web browser). Our current page simplecarloancalculator.php will be reloaded and we will have access to the data entered into the form in an array named $_POST.
To be able to use the data entered into the car price text box we use $_POST[carPrice], where carPrice is the name used in the form above. Since we in reality are using the PHP code before the form is created we will place the code above the form.
PHP coding
We will start off with two functions and one variable.
isset() – function to test if variable is set [returns true/false].
empty() – function to test if the variable is empty [returns true/false].
$carPrice – variable to store the car price in.
Looks like isset() and empty() are doing pretty much the same but I will soon explain the slightly but very important difference.
Let us examine a code snippet.
if (isset($_POST[‘carPrice’]) && !empty($_POST[‘carPrice’]))
$carPrice = check_input($_POST[‘carPrice’]);
else
$carPrice = 0;
isset($_POST[‘carPrice’]) –> If something was posted in texbox named carPrice (will return true even if an empty box was posted).
empty($_POST[‘carPrice’]) –> If nothing is in $_POST[‘carPrice’] (will return true first time the page is loaded).
Combined together the expressions (please notice the ! before empty function) will be evaluated as:
If something was typed in the text box named carPrice and the box was not empty. Variable $carPrice
will be set to that something, otherwise set variable $carPrice to 0.
The same procedure will needed for term and interestRate as well, creating variables $term and $interestRate, but that code will not be repeated here.
Time to do the mathematical work.
We will next create a function taking the three input parameters $totalLoan, $years and $interest. The function will then return the cost per month rounded off to whole dollars.
function calculateMonthlyAmortizingCost($totalLoan, $years, $interest )
$tmp = pow((1 + ($interest / 1200)), ($years * 12));
return round(($totalLoan * $tmp) * ($interest / 1200) / ($tmp – 1));
Next step will be using our newly created function and passing our variables as arguments.
$monthlyCost = calculateMonthlyAmortizingCost($carPrice, $term, $interestRate);
And we are done! Almost, we need to print the price on the web page. To do that we will use the echo function that outputs text to the web page.
echo($monthlyCost)
You may also like
Archives
- November 2025
- October 2025
- September 2025
- August 2025
- July 2025
- June 2025
- May 2025
- April 2025
- March 2025
- February 2025
- January 2025
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- February 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- June 2021
- May 2021
- April 2021
- March 2021
- February 2021
- January 2021
- December 2020
- November 2020
- October 2020
- September 2020
- August 2020
- July 2020
- June 2020
- May 2020
- April 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- January 2017
- July 2016
- June 2016
- April 2016
- February 2016
- June 2015
- July 2014
- February 2014
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | |

Leave a Reply
You must be logged in to post a comment.