PHP中的PMT功能

时间:2014-10-27 11:05:59

标签: php html

我一直在尝试将PMT功能实现为PHP以用作贷款计算器。

PHP代码无效,我不太清楚为什么,请注意我是一个完全的初学者。

PHP(Get.php):

<?php
    $loan = $_POST["loanamount"];
    $interest = $_POST["interest"];
    $months = $_POST["months"];

   function pmt($interest, $months, $loan) {
       $months = $months;
       $interest = $interest / 1200;
       $amount = $interest * -$loan * pow((1 + $interest), $months) / (1 - pow((1 + $interest), $months));
       return number_format($amount, 2);
    }

    echo "Your payment will be &pound;" . round($amount,2) . " a month, for " . $months . " months";

?>

HTML:

<form action="get.php" method="post">

    Loan Amount: <input type="text" min="1" name="loanamount"><br />
    Intrest: <input type="text" min="1" name="interest"><br />
    Months: <input type="text" min="1" name="months"><br />
    <input type="submit" value="Submit">
    Monthly repayment: 


</form>

提前感谢你。

2 个答案:

答案 0 :(得分:5)

您创建了函数pmt,但您从未在任何地方调用它。

要实现此功能,您实际上需要调用pmt函数。

这样的事情

<?php
    $loan = $_POST["loanamount"];
    $interest = $_POST["interest"];
    $months = $_POST["months"];

   function pmt($interest, $months, $loan) {
       $months = $months;
       $interest = $interest / 1200;
       $amount = $interest * -$loan * pow((1 + $interest), $months) / (1 - pow((1 + $interest), $months));
       return number_format($amount, 2);
    }

    $amount = pmt($interest, $months, $loan);
    echo "Your payment will be &pound;" . round($amount,2) . " a month, for " . $months . " months";

?>

答案 1 :(得分:2)

您没有调用pmt()功能。在echo语句之前添加以下行:

$amount = pmt($interest, $months, $loan);