<?php
 
// Staring straight up into the sky ... oh my my
error_reporting(-1);
mb_internal_encoding('utf-8');
/* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
//function inclineWord($number, $word1, $word2, $word5) {
function inclineWord($number) {
    if (($number % 100 >= 10) && ($number % 100 <= 20)) {
        $number = " рублей";
    }
    elseif (($number % 10 == 0) || ($number % 10 >= 5)) {
     	$number = " рублей";
    }
    elseif (($number % 10 >= 2) && ($number % 10 <= 4)) {
      	$number = " рубля";
    }
    else {
     	$number = " рубль";
    }
    return $number;
}
    /* 
            Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю, 
            если мы считаем число для мужского рода (один рубль), 
            и 1 — для женского (одна тысяча) 
    */
 
    //function smallNumberToText($number, $isFemale) {
    function smallNumberToText($number) {
        $spelling = 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  =>  'девятнадцать'    
        );
 
        $arr = array();
        
        if ($number >= 1000000) {
    		$million = floor($number / 1000000);
            $x = $number % 1000000;
            $thousand = floor($x / 1000);
            $hundred = $number % 1000;
            $arr = array($million, $thousand, $hundred);
            if (($million % 100 >= 10) && ($million % 100 <= 20)) {
        $mill = " миллионов";
    }
    elseif (($million % 10 == 1) || ($million % 10 >= 5)) {
     	$mill = " миллионов";
    }
    elseif (($million % 10 >= 2) && ($million % 10 <= 4)) {
      	$mill = " миллиона";
    }
    else {
     	$mill = " миллион";
    }
        
            //var_dump($arr);
        }
 
    	elseif ($number >= 1000) {
    		$thousand = floor($number / 1000);
    		$hundred = $number % 1000;
    		$arr = array($thousand, $houndred); 
    		$numb = ' тысяч ';
    		// var_dump($arr);
             	
    	}
        
        foreach ($arr as $value) {
            if ($value >= 100) {
                $mod = $value % 100;
                $sotni = $value - $mod;
                //$value = $spelling[$sotni];
                $mod2 = $value % 10;
                $desyatki = $mod - $mod2;
                $word = $word . ' ' . $spelling[$sotni] . ' ' . $spelling[$desyatki] . ' ' . $spelling[$mod2];
                
            } 
            elseif ($value > 9) {
        	    $mod = $value % 10;
                $sotni = $value - $mod;
                //$value = $spelling[$sotni];
                $desyatki = $mod - $mod2;
                $word = $word . ' ' . $spelling[$mod];
            }
            else {
            	$word = $word . $spelling[$value];
            } 
            
        }
        
        $number = $word;
        return $number;
        
        /*
           $femaleSpelling = array(
                1   =>  'одна',        2   =>  'две'
            );
         */
    }
 
    function numberToText($number) {
 
        $number = smallNumberToText($number) . ' ' . '(' . $number . ')' . inclineWord($number);
        return $number;
    }
 
 
    $amount0 = 3222777;
    //$amount0 = splitNumber($amount0);
    $text0 = numberToText($amount0);
    echo "На вашем счету {$text0}\n";
 /*
        /* Вызовем функцию несколько раз */
       // $amount1 = mt_rand(1,99999999);
      //  $text1 = numberToText($amount1);
 
        //echo "На вашем счету {$text1}\n";
 
        //$amount2 = mt_rand(1,99999999);
       // $text2 = numberToText($amount2);
 
        //echo "На вашем счету {$text2}\n";
 
        //$amount3 = mt_rand(1,99999999);
       // $text3 = numberToText($amount3);
 
        //echo "На вашем счету {$text3}\n";
 
        //$amount4 = mt_rand(1,99999999);
        //$text4 = numberToText($amount4);
 
        //echo "На вашем счету {$text4}\n";