Wallogit.com
2017 © Pedro Peláez
Calculates the monthly repayments of a loan
Provides some functions to:, (*1)
Also provides a method to return the loan payment schedule., (*2)
composer require pqrs/phploan=dev-master
Alternatively, add the dependency directly to your composer.json file:, (*3)
"require": {
"pqrs/phploan": "dev-master"
}
Then add to your php code:, (*4)
``` php require_once DIR . '/vendor/autoload.php'; // Autoload files using Composer autoload, (*5)
use PHPLoan\Loan;, (*6)
## Usage
Variables nomenclature, (*7)
$P Principal (loan amount) $r Annual interest rate $n Number of payments (months) $A Monthly payment amount, (*8)
### method calculateMonthlyPayment( $P, $r, $n ) Calculates the amount of every monthly payment based on the loan principal, interest rate and number of payments. ``` php $loan = new Loan; $result = $loan->calculateMonthlyPayment( 60000 , 9, 360 ); echo number_format($result, 2, ".", ",") . PHP_EOL; // Prints 482.77
Calculates the loan principal based on the number of payments, interest rate and payment amount., (*9)
``` php $loan = new Loan;, (*10)
$result = $loan->calculatePrincipal( 360, 9, 482.77357 ); echo number_format($result, 2, ".", ",") . PHP_EOL;, (*11)
// Prints 60,000.00, (*12)
### method calculateNumPayments( $P, $r, $A ) Calculates the number of payments based on the principal, interest rate and payment amount. ``` php $loan = new Loan; $result = $loan->calculateNumPayments( 60000, 9, 482.77357 ); echo $result . PHP_EOL; // Prints 360
Returns an object with the loan payment schedule., (*13)
``` php $loan = new Loan;, (*14)
$schedule = $loan->getSchedule( 60000, 9, 12 );, (*15)
echo ", (*16)
| " . $key->numpayment . " | "; echo "" . $key->payment . " | "; echo "" . $key->interest . " | "; echo "" . $key->principal . " | "; echo "" . $key->balance . " | "; echo "
";, (*17)
// Prints html table: // 1 5,247.09 450.00 4,797.09 55,202.91 // 2 5,247.09 414.02 4,833.07 50,369.84 // 3 5,247.09 377.77 4,869.31 45,500.53 // 4 5,247.09 341.25 4,905.83 40,594.70 // 5 5,247.09 304.46 4,942.63 35,652.07 // 6 5,247.09 267.39 4,979.70 30,672.37 // 7 5,247.09 230.04 5,017.05 25,655.32 // 8 5,247.09 192.41 5,054.67 20,600.65 // 9 5,247.09 154.50 5,092.58 15,508.07 // 10 5,247.09 116.31 5,130.78 10,377.29 // 11 5,247.09 77.83 5,169.26 5,208.03 // 12 5,247.09 39.06 5,208.03 0.00, (*18)
## Examples You can find some uses for these functions in [tests folder](tests). ## Prerequisites PHP > 5.6 Not tested, but if you want to use phploan in 5.x PHP versions lower than 5.6, probably the only thing you have to change is the use of exponential expressions in [Loan.php](src/PHPLoan/Loan.php). Change ** for the function **pow()**: ``` php // In method calculateMonthlyPayment( $P , $r, $n ) $A = $P * (( $i * pow((1 + $i), $n) ) / ( pow((1 + $i), $n) - 1 )); // In method calculatePrincipal( $n, $r, $A ) $P = $A * ( pow((1 + $i), $n) - 1 ) / ( $i * (pow((1 + $i), $n) ));
Contributions are very welcome!, (*19)
Copyright © 2018 Alvaro Piqueras - pqrs, (*20)
This project is licensed under the MIT License - see the LICENSE file for details., (*21)