<?php

error_reporting(-1);

$creditBalance = 40000;
$percent = 1.03;
$serviceFee = 1000;
$monthlyPayment = 5000;
$paymentTotal = 0;

for ($months = 1; $months <= 20; $months ++) {
    $creditBalance = ($creditBalance * $percent) + $serviceFee - $monthlyPayment;
    $paymentTotal = $paymentTotal + $monthlyPayment;
    echo "At the end of the month #{$months}: debt = {$creditBalance}, total payment by now = {$paymentTotal} \n";

    if (($creditBalance * $percent) + $serviceFee < 5000) {
    
    $paymentTotal = $paymentTotal + ($creditBalance * $percent) + $serviceFee;
    $months++;
    break;
}
}

echo "Eventually in the month #{$months} the boy has totally paid {$paymentTotal}";
