Let us see how to write a simple PHP program to calculate EMI. This program can be used to calculate Home Loans, Car Loans & Personal loans, etc.,
Here is the PHP function to calculate EMI:
// Function to calculate EMI
function calculateEMI($p, $r, $n) {
$r = $r/(12*100); // monthly interest rate
$emi = ($p*$r*pow(1+$r,$n))/(pow(1+$r,$n)-1);
return $emi;
}
I have used this Formula: EMI = [P x R x (1+R)^N]/[(1+R)^N-1], where P is the principal amount, R is the monthly interest rate, and N is the number of months.
Let me write a complete example to test this script using PHP and HTML.
<?php
// Function to calculate EMI
function calculateEMI($p, $r, $n) {
$r = $r/(12*100); // monthly interest rate
$emi = ($p*$r*pow(1+$r,$n))/(pow(1+$r,$n)-1);
return $emi;
}
//if the form is submitted
if (isset($_POST['submit'])) {
// Inputs
$principal = $_POST['principal'];
$rate = $_POST['rate'];
$time = $_POST['time'];
// Calculate EMI
$emi = calculateEMI($principal, $rate, $time);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>EMI Calculator</title>
</head>
<body>
<form action="" method="post">
<label for="principal">Loan Amount:</label>
<input type="text" id="principal" name="principal" required><br /><br />
<label for="rate">Annual Interest Rate:</label>
<input type="text" id="rate" name="rate" required> <br /><br />
<label for="time">Loan Tenure (in Months):</label>
<input type="text" id="time" name="time" required> <br /><br />
<input type="submit" name="submit" value="Calculate EMI">
</form>
<br />
<?php
if (isset($emi)) {
echo "EMI: ".$emi;
}
?>
</body>
</html>Output:
