Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
CREATE TABLE calculations (
id INT AUTO_INCREMENT PRIMARY KEY,
home_value DECIMAL(10, 2),
down_payment DECIMAL(10, 2),
loan_term INT,
interest_rate DECIMAL(5, 2),
loan_amount DECIMAL(10, 2),
monthly_payment DECIMAL(10, 2),
total_interest DECIMAL(10, 2),
total_cost DECIMAL(10, 2),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
<?php
function calculateMortgage($homeValue, $downPayment, $loanTerm, $interestRate) {
$loanAmount = $homeValue - $downPayment;
$monthlyInterestRate = $interestRate / 100 / 12;
$numberOfPayments = $loanTerm * 12;
if ($monthlyInterestRate == 0) {
$monthlyPayment = $loanAmount / $numberOfPayments;
} else {
$monthlyPayment = $loanAmount * ($monthlyInterestRate * pow(1 + $monthlyInterestRate, $numberOfPayments)) /
(pow(1 + $monthlyInterestRate, $numberOfPayments) - 1);
}
$totalPayment = $monthlyPayment * $numberOfPayments;
$totalInterest = $totalPayment - $loanAmount;
return [
'loanAmount' => round($loanAmount, 2),
'monthlyPayment' => round($monthlyPayment, 2),
'totalInterest' => round($totalInterest, 2),
'totalCost' => round($totalPayment, 2),
];
}
// Пример ввода
$homeValue = 300000; // стоимость жилья
$downPayment = 60000; // первоначальный взнос
$loanTerm = 30; // срок кредита в годах
$interestRate = 3.5; // процентная ставка
$result = calculateMortgage($homeValue, $downPayment, $loanTerm, $interestRate);
echo "Сумма ипотечного кредита: " . $result['loanAmount'] . "\n";
echo "Ежемесячный платеж: " . $result['monthlyPayment'] . "\n";
echo "Переплата по кредиту: " . $result['totalInterest'] . "\n";
echo "Полная стоимость кредита: " . $result['totalCost'] . "\n";
?>