3v4l.org

run code in 300+ PHP versions simultaneously
<?php $number = 1000000; echo convert_number_to_words($number); function convert_number_to_words($number) { $hyphen = '-'; $conjunction = ' and '; $separator = ', '; $negative = 'negative '; $decimal = ' point '; $dictionary = array( 0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten', 11 => 'eleven', 12 => 'twelve', 13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen', 16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen', 19 => 'nineteen', 20 => 'twenty', 30 => 'thirty', 40 => 'fourty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety', 100 => 'hundred', 1000 => 'thousand', 100000 => 'lakhs', 10000000 => 'cores', 1000000000 => 'billion', 1000000000000 => 'trillion', 1000000000000000 => 'quadrillion', 1000000000000000000 => 'quintillion' ); if (!is_numeric($number)) { return false; } if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) { // overflow trigger_error( 'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX, E_USER_WARNING ); return false; } if ($number < 0) { return $negative . convert_number_to_words(abs($number)); } $string = $fraction = null; if (strpos($number, '.') !== false) { list($number, $fraction) = explode('.', $number); } switch (true) { case $number < 21: $string = $dictionary[$number]; break; case $number < 100: $tens = ((int) ($number / 10)) * 10; $units = $number % 10; $string = $dictionary[$tens]; if ($units) { $string .= $hyphen . $dictionary[$units]; } break; case $number < 1000: $hundreds = $number / 100; $remainder = $number % 100; $string = $dictionary[$hundreds] . ' ' . $dictionary[100]; if ($remainder) { $string .= $conjunction . convert_number_to_words($remainder); } break; default: $pow = floor(log($number, 10)); $remainer_val = 100; if($pow >= 3 && $pow < 5 ) { $baseUnit = pow(1000, floor(log($number, 1000))); } else if($pow >= 5 && $pow < 7 ) { $baseUnit = 100000; $remainer_val = 10; } else if($pow >= 7 && $pow < 9 ) { $baseUnit = 10000000; $remainer_val = 10; } else { $baseUnit = pow(1000, floor(log($number, 1000))); } $numBaseUnits = (int) ($number / $baseUnit); $remainder = $number % $baseUnit; $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit]; if ($remainder) { $string .= $remainder < $remainer_val ? $conjunction : $separator; $string .= convert_number_to_words($remainder); } break; } if (null !== $fraction && is_numeric($fraction)) { $string .= $decimal; $words = array(); foreach (str_split((string) $fraction) as $number) { $words[] = $dictionary[$number]; } $string .= implode(' ', $words); } echo $string; }
Output for 4.4.0 - 4.4.9, 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
ten lakhs
Output for 4.3.0 - 4.3.11, 5.0.0 - 5.0.4
Notice: Use of undefined constant PHP_INT_MAX - assumed 'PHP_INT_MAX' in /in/OoIZ0 on line 54 Notice: Use of undefined constant PHP_INT_MAX - assumed 'PHP_INT_MAX' in /in/OoIZ0 on line 54 ten lakhs

preferences:
288.49 ms | 402 KiB | 453 Q