<?php
 
error_reporting(-1);
 
/* требуемая сумма */
$amount = 54500;
$totalPay = 0;
$i = 0;
 
$hundreds = 0;
$fiveHundreds = 0;
$thousand = 0;
$fiveThousand = 0;
 
$bills = array(
    100 => 23,
    500 => 5,
    1000 => 0,
    5000 => 200
);
 
do {
    if ($bills[100] > $hundreds) {
        $hundreds++;
    } elseif ($bills[500] > $fiveHundreds){
        $hundreds = 0;
        $fiveHundreds++;
    } elseif ($bills[1000] > $thousand) {
        $hundreds = 0;
        $fiveHundreds = 0;
        $thousand++;
    } elseif ($bills[5000] > $fiveThousand) {
        $hundreds = 0;
        $fiveHundreds = 0;
        $thousand = 0;
        $fiveThousand++;
    }
    $i++;
    $totalPay = $hundreds * 100 + $fiveHundreds * 500 + $thousand * 1000 + $fiveThousand * 5000;
    echo "шаг №{$i}. TotalPay = {$totalPay} \n";
} while ($amount != $totalPay);
 
echo "Необходимо выдать {$hundreds} x 100, {$fiveHundreds} x 500, {$thousand} x 1000, {$fiveThousand} x 5000. Выдано {$totalPay}";