<?php
 
error_reporting(-1);
mb_internal_encoding('utf-8');
function spellSmallNumber($number, $female = null){
    $last2Digits = $number % 100;
    $lastDigit = $number % 10;
    $hundreds = $number - $last2Digits;
    $decades = $last2Digits - ($number % 10);
    $words = [];
    $femaleSpelling = array(
        1   =>  'одна',        2   =>  'две'
    );
    $numbertoWords = array(
        0   =>  'ноль',                                      10  =>  'десять',       100 =>  'сто',
        1   =>  'один',         11  =>  'одиннадцать',      20  =>  'двадцать',     200 =>  'двести',
        2   =>  'два',          12  =>  'двенадцать',       30  =>  'тридцать',     300 =>  'триста',
        3   =>  'три',          13  =>  'тринадцать',       40  =>  'сорок',        400 =>  'четыреста',
        4   =>  'четыре',       14  =>  'четырнадцать',     50  =>  'пятьдесят',    500 =>  'пятьсот',
        5   =>  'пять',         15  =>  'пятнадцать',       60  =>  'шестьдесят',   600 =>  'шестьсот',
        6   =>  'шесть',        16  =>  'шестнадцать',      70  =>  'семьдесят',    700 =>  'семьсот',    
        7   =>  'семь',         17  =>  'семнадцать',       80  =>  'восемьдесят',   800 =>  'восемьсот',
        8   =>  'восемь',       18  =>  'восемнадцать',     90  =>  'девяносто',     900 =>  'девятьсот',
        9   =>  'девять',       19  =>  'девятнадцать'    
	);
 
    if($number == 0){
    	return $numbertoWords[$number];  
    }else{
    	if($hundreds != 0){ //Сотни
    		$words[] = $numbertoWords[$hundreds];
    	}  
        if($last2Digits >= 10 && $last2Digits <= 19){           // 10-19
    	    $words[] = $numbertoWords[$last2Digits];
        }else{
            if($decades != 0){ //Десятки
            	$words[] = $numbertoWords[$decades];
            }  
            if($female == 1 && ($lastDigit == 1 || $lastDigit == 2)){
    	    $words[] = $femaleSpelling[$lastDigit];//Единицы
            }else{
                if($lastDigit != 0){
                	$words[] = $numbertoWords[$lastDigit];
                }
            }  
        }
    }
        $result = implode(" ", $words);
        return $result;
}
 
function inclineWord ($number, $word1, $word2, $word5){
	$last2Digits = $number % 100;
	$lastDigit = $number % 10;
 
	if($last2Digits >= 11 && $last2Digits <=14){
		return $word5;
	}elseif($lastDigit == 1){
		return $word1;
	}elseif($lastDigit >= 2 && $lastDigit <= 4){     
		return $word2;
	}else{
		return $word5;
	}
}
 
function spellNumber($number){
	$words = [];
	$millions = floor($number / 1000000);
	$thousands = floor($number / 1000 % 1000);
	$units = $number % 1000;
	$millionstoWords = spellSmallNumber($millions) ." ". inclineWord($millions, "миллион ", "миллиона ", "миллионов ");
	$thousandstoWords = spellSmallNumber($thousands, 1) ." ". inclineWord($thousands, "тысяча ", "тысячи ", "тысяч ");
	$unitstoWords = spellSmallNumber($units) ." ". inclineWord($units, "рубль ", "рубля ", "рублей ");
	if($millions == 0){
	}else{
		$words[] = $millionstoWords;
	}
	if($thousands == 0){
	}else{
		$words[] = $thousandstoWords;
	}
	if($units == 0 && $number != 0){
		$words[] = inclineWord($units, "рубль ", "рубля ", "рублей ");
	}else{
	    $words[] = $unitstoWords;
	}    
	$result = implode($words);
	return $result;
}	
 
$amount1 = mt_rand(1,99999999);
$text1 = spellNumber($amount1);
echo "На вашем счету {$text1}\n";
 
$amount2 = mt_rand(1,99999999);
$text2 = spellNumber($amount2);
 
echo "На вашем счету {$text2}\n";
 
$amount3 = mt_rand(1,99999999);
$text3 = spellNumber($amount3);
 
echo "На вашем счету {$text3}\n";
 
$amount4 = mt_rand(1,99999999);
$text4 = spellNumber($amount4);
 
echo "На вашем счету {$text4}\n";