2014-10-27 58 views
0

我一直在嘗試將PHP中的PMT函數作爲貸款計算器來使用。PHP中的PMT函數

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

因爲你永遠不會真的*調用* pmt()函數。 – deceze 2014-10-27 11:12:14

回答

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

完美的作品,謝謝! – 2014-10-27 11:20:23

+3

我不是PHP專家,但'$ months = $ months;'看起來有點無用。 – AsTeR 2015-11-13 11:28:23

2

你不是叫你pmt()功能。在你的echo語句之前添加以下行:

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

感謝您的幫助 – 2014-10-27 11:20:55