First we must create a brand new PHP file: simplecarloancalculator.php. A PHP file is handled by the online server as a traditional HTML file aside from the code written inside a php tag.
We begin off by creating the automobile mortgage calculator HTML type submitting knowledge again to this internet web page.
Rate of interest:
The code above will create a type containing three textual content bins and a button.
Automobile value: ___ Time period: ___ Rate of interest: ___ [Calculate]
Could be translated to:
When the calculate button is pressed the info within the textual content bins can be despatched to the web page named: simplecarloancalculator.php (the web page we’ve got all prepared have loaded in our internet browser). Our present web page simplecarloancalculator.php can be reloaded and we can have entry to the info entered into the shape in an array named $_POST.
To have the ability to use the info entered into the automobile value textual content field we use $_POST[carPrice], the place carPrice is the identify used within the type above. Since we in actuality are utilizing the PHP code earlier than the shape is created we’ll place the code above the shape.
We are going to begin off with two features and one variable.
isset() – operate to check if variable is ready [returns true/false].
empty() – operate to check if the variable is empty [returns true/false].
$carPrice – variable to retailer the automobile value in.
Seems like isset() and empty() are doing just about the identical however I’ll quickly clarify the marginally however crucial distinction.
Allow us to study a code snippet.
if (isset($_POST[‘carPrice’]) && !empty($_POST[‘carPrice’]))
isset($_POST[‘carPrice’]) –> If one thing was posted in texbox named carPrice (will return true even when an empty field was posted).
empty($_POST[‘carPrice’]) –> If nothing is in $_POST[‘carPrice’] (will return true first time the web page is loaded).
Mixed collectively the expressions (please discover the ! earlier than empty operate) can be evaluated as:
If one thing was typed within the textual content field named carPrice and the field was not empty. Variable $carPrice
can be set to that one thing, in any other case set variable $carPrice to zero.
The identical process will wanted for time period and interestRate as nicely, creating variables $time period and $interestRate, however that code won’t be repeated right here.
Time to do the mathematical work.
We are going to subsequent create a operate taking the three enter parameters $totalLoan, $years and $curiosity. The operate will then return the associated fee per thirty days rounded off to complete .
operate calculateMonthlyAmortizingCost($totalLoan, $years, $curiosity )
Subsequent step can be utilizing our newly created operate and passing our variables as arguments.
$monthlyCost = calculateMonthlyAmortizingCost($carPrice, $time period, $interestRate);
And we’re finished! Virtually, we have to print the value on the internet web page. To do this we’ll use the echo operate that outputs textual content to the online web page.