Car Loan Calculator – Php Basic Programming

Girls 4-12 SONOMA Goods for Life™ Sequin Pocket Cardigan
With sequin-covered pockets, this girls' SONOMA Goods for Life cardigan is a stand-out fave.

Womens WallFlower Plus Dresses, Clothing | Kohl's
Enjoy free shipping and easy returns every day at Kohl's. Find great deals on Womens WallFlower Plus Dresses at Kohl's today!

First we will have to create a new PHP file: simplecarloancalculator.php. A PHP file is rated 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: The code above will create a form containing three text boxes and a button.

 Car price: ___
 Term: ___
 Interest rate: ___

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']);
$ 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 need 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)

Sonoma Goods For Life Wide Spring Ankle Boots - Shoes | Kohl's
Enjoy free shipping and easy returns every day at Kohl's. Find great deals on Sonoma Goods For Life Wide Spring Ankle Boots at Kohl's today!

LifeStride Devyn Women's Pumps
A polished pump with the perfect height, you'll love wearing these LifeStride Devyn for a tailored look.

Clarks® Layton Step Women's Sneakers
You'll look stylish in these Clarks Layton Step women's sneakers.

Belle Maison White Month Dry Erase Board Wall Decor
Enjoy effortless style with this Belle Maison White Month Dry Erase Board Wall Decor.

Xray Maverik Boys' Ankle Boots
These Xray Maverik boys' ankle boots will be his new favorite pair.

Rio Brands Personal Beach Table
Easily keep your food and drinks out of the sand during your next beach outing with this stylish Rio Brands personal beach table.

Mellow World Itzel Colorblock Tote Bag
This Mellow World Itzel tote bag features a colorblock design and tassel accent.

You may also like...