<?php
function numberToWords($num)
{
if ($num == 0) {
return "Zero";
}
$words = array(
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"
);
$tens = array(
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety"
);
$thousands = array(
"",
"Thousand",
"Million",
"Billion"
);
$words_str = "";
$segment = 0;
while ($num > 0) {
$chunk = $num % 1000;
if ($chunk != 0) {
$chunk_words = "";
if ($chunk >= 100) {
$chunk_words .= $words[(int) ($chunk / 100) - 1]
. " Hundred ";
$chunk %= 100;
}
if ($chunk >= 20) {
$chunk_words .= $tens[(int) ($chunk / 10) - 2] . " ";
$chunk %= 10;
}
if ($chunk > 0) {
$chunk_words .= $words[$chunk - 1] . " ";
}
$words_str = trim($chunk_words) . " "
. $thousands[$segment] . " " . $words_str;
}
$num = (int) ($num / 1000);
$segment++;
}
return trim($words_str);
}
$num1 = 958237764;
$num2 = 55000000;
echo "Input: $num1\n";
echo "Output: " . numberToWords($num1) . "\n\n";
echo "Input: $num2\n";
echo "Output: " . numberToWords($num2) . "\n";