<?php

error_reporting(-1);

function countTotalPayment ($creditDebt, $percent, $serviceFee, $monthlyPayment) {
    for ($months = 2, $paymentTotal = 0; $months <= 20; $months ++) {
        $creditDebt = ($creditDebt * $percent) + $serviceFee - $monthlyPayment;
        $paymentTotal = $paymentTotal + $monthlyPayment;
    if ($serviceFee == 7777 && $months >= 3) {
        $creditDebt = $creditDebt - $serviceFee;
}
    if (($creditDebt * $percent) + $serviceFee < $monthlyPayment) {
        $paymentTotal = $paymentTotal + ($creditDebt * $percent) + $serviceFee;
        $months++;
        break;
}
}
    return $paymentTotal;
}

$creditSum = 40000;
$payout = 5000;
$homoCreditTotal = countTotalPayment ($creditSum, 1.04, 500, $payout);
$softbankTotal = countTotalPayment ($creditSum, 1.03, 1000, $payout);
$strawberryBankTotal = countTotalPayment ($creditSum, 1.02, 7777, $payout);

echo "HomoCredit: {$homoCreditTotal} \n";
echo "Softbank: {$softbankTotal} \n";
echo "StrawberryBank: {$strawberryBankTotal} \n";