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:
1 2 3 4 5 6 |
// 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?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: