Car Insurance Calculator – Php Basic Programming

First, we will have to create a new PHP file: simpleecarinsurancecalculator.php. The PHP file is treated by the web server as a normal HTML file except for the code written inside the php tag.

We start by creating an HTML form for the car insurance calculator to submit the data back to this web page.

Vehicle Price: Duration: Interest Rate: The above code will generate a form with three text boxes and a button.

Car price: ___
Term: ___
Interest rate: ___

It can be translated to:

When you press the Calculate button, the data in the text boxes will be sent to the page called: simpleecarinsurancecalculator.php (the page we all loaded in our web browser). Our current page simpleecarinsurancecalculator.php will be reloaded and we will have access to the data entered in the form in an array called $_POST.

To be able to use the data entered in the car price text box, we use $_POST[carPrice], where carPrice is the name used in the above form. Since we are actually using PHP code before creating the form, we will place the code on top of the form.

PHP encoding

We’ll start with two functions and one variable.

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

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

Vehicle price in US dollars – a variable to store the vehicle price in.

isset() and blank() seem to do almost the same thing but I’ll explain shortly the slight but very important difference.

Let’s examine the code snippet.

IF (array ($_POST .)[‘carPrice’]) &&! Empty ($_ POST[‘carPrice’]))


car price $ = check_input ($ _ POST[‘carPrice’]);




car price $0;


isset ($_POST[‘carPrice’]) -> if something is posted in texbox named carPrice (it will return true even if an empty box is posted).

Empty ($_ POST[‘carPrice’]) -> if there is nothing in $_POST[‘carPrice’] (It will return true the first time the page is loaded).

Merging expressions together (please note! before an empty function) will evaluate as follows:

If something is typed into the text box called carPrice and the box is not empty. car price variable $

It will be set to this thing, otherwise set the $carPrice variable to 0.

It would take the same procedure for term and interest rate as well, creating the variables $term and $InterestRate, but that code won’t be duplicated here.

It’s time to do the math.

We will then create a function with the three input parameters $totalInsurance, $years and $Interest. The job will then return the cost per month, rounded up to full dollars.

calculateMonthlyAmortizingCost function ($totalInsurance, in dollar years, in dollar interest)


$tmp = pow ((1 + ($Interest / 1200)), ($ year * 12));

Round return ((totalInsurance * $tmp) * ($Interest / 1200) / ($tmp – 1));


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

$MonthlyCost = calculateMonthlyAmortizingCost ($carPrice, $term, $InterestRate);

And we’re done! Roughly, we need to print the price on the webpage. To do this, we will use the echo function which outputs the text to the web page.

Echo (monthly cost in dollars)