Business

Car Loan Calculator – Basic Php Programming

Posted by admin

First we will need to create a new PHP file: simplecarloancalculator.php. The web server treats a PHP file like a normal HTML file, except for the code written inside a php tag.

We begin by creating the Auto Loan Calculator HTML form that submits data to this web page.

Car Price: Term: Interest Rate: The above code will create a form containing three text boxes and a button.

Car price: ___
Term: ___
Interest rate: ___
[Calculate]



It can be translated to:

When the calculate button is pressed, the data in the text boxes will be sent to the page called: simplecarloancalculator.php (the page we already have loaded in our web browser). Our current simplecarloancalculator.php page will reload and we will have access to the data entered into the form in an array named $_POST.

In order to use the data entered in the car price text box, we use $_POST[carPrice], where carPrice is the name used in the previous form. Since we’re actually using the PHP code before the form is created, we’ll place the code on top of the form.

PHP coding

We’ll start with two functions and one variable.

isset() – function to test if the variable is set [returns true/false].

void() – function to test if variable is empty [returns true/false].

$carPrice: variable to store the price of the car.

It looks like isset() and void() are doing pretty much the same thing, but I’ll explain the slight but very important difference soon.

Let’s review a code snippet.

if (isset($_POST[‘carPrice’]) && !empty($_POST[‘carPrice’]))

{

$carPrice = check_input($_POST[‘carPrice’]);

}

the rest

{

$carprice = 0;

}

isset($_POST[‘carPrice’]) –> If something was posted in the textbox named carPrice (will return true even if an empty box was posted).

void($_POST[‘carPrice’]) –> If there is nothing in $_POST[‘carPrice’] (will return true the first time the page loads).

Combined, the expressions (note the ! before the empty function) will evaluate to:

If something was typed in the text box named carPrice and the box was not empty. Variable $carPrice

it will be established in that something; otherwise, set the $carPrice variable to 0.

The same procedure will be needed for the term and the interest rate, creating the variables $term and $interestrate, but that code will not be repeated here.

It’s time to do the math work.

Next, we’ll create a function taking the three input parameters $totalLoan, $years, and $interest. The function will then return the cost per month rounded up to whole dollars.

function calculateMonthlyAmortizingCost($totalLoan, $years, $interest )

{

$tmp = pow((1 + ($interest / 1200)), ($years * 12));

return round(($totalLoan * $tmp) * ($interest / 1200) / ($tmp – 1));

}

The next step will be to use our newly created function and pass our variables as arguments.

$monthlycost = computeMonthlyAmortizationCost($carprice, $term, $interestrate);

And we’re done! Almost, we need to print the price on the web page. To do this, we will use the echo function that sends text to the web page.

echo ($costmonthly)

Leave A Comment