<?php

error_reporting(-1);

function getCostPhone($cost, $monthlyPayment, $percent, $commission = 0, $openPayment = 0)
{
	$credit = $cost + $openPayment;
	$totalPayment = 0;

	for ($month = 1; $credit > 0; $month++) {
		$credit = $credit * $percent + $commission;
		if ($credit > $monthlyPayment) {
			$totalPayment += $monthlyPayment;
			$credit -= $monthlyPayment;
		} else {
			$totalPayment += $credit;
			$credit = 0;
		}
	}

	return $totalPayment;
}

$homoCredit = getCostPhone(39999, 5000, 1.04, 500);
$softBank = getCostPhone(39999, 5000, 1.03, 1000);
$strawberryBank = getCostPhone(39999, 5000, 1.02, 0, 7777);

echo "homoCredit: {$homoCredit} руб.\n";
echo "softBank: {$softBank} руб.\n";
echo "strawberryBank: {$strawberryBank} руб.\n";