How To Convert Number To Words in PHP Example Tutorial

Reading Time: 5 minutes
2,002 Views

Inside this article we will see the concept i.e How To Convert Number To Words in PHP Example Tutorial. Article contains the classified information about Number to Words conversion in PHP.

If you are looking for a solution i.e How to Convert number to words in PHP then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Number To Words means:

120: One Hundred Twenty

4500: Four Thousand and Five Hundreds

Learn More –

Let’s get started.

Application Programming

Create a folder say number-to-word in your localhost directory. Create a file index.php into it.

Open index.php file and write this code into it.

<?php

// Function which returns number to words
function numberToWord($num = '')
{
    $num    = (string) ((int) $num);

    if ((int) ($num) && ctype_digit($num)) {
        $words  = array();

        $num    = str_replace(array(',', ' '), '', trim($num));

        $list1  = array(
            '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
            'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
            'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
        );

        $list2  = array(
            '', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty',
            'seventy', 'eighty', 'ninety', 'hundred'
        );

        $list3  = array(
            '', 'thousand', 'million', 'billion', 'trillion',
            'quadrillion', 'quintillion', 'sextillion', 'septillion',
            'octillion', 'nonillion', 'decillion', 'undecillion',
            'duodecillion', 'tredecillion', 'quattuordecillion',
            'quindecillion', 'sexdecillion', 'septendecillion',
            'octodecillion', 'novemdecillion', 'vigintillion'
        );

        $num_length = strlen($num);
        $levels = (int) (($num_length + 2) / 3);
        $max_length = $levels * 3;
        $num    = substr('00' . $num, -$max_length);
        $num_levels = str_split($num, 3);

        foreach ($num_levels as $num_part) {
            $levels--;
            $hundreds   = (int) ($num_part / 100);
            $hundreds   = ($hundreds ? ' ' . $list1[$hundreds] . ' Hundred' . ($hundreds == 1 ? '' : 's') . ' ' : '');
            $tens       = (int) ($num_part % 100);
            $singles    = '';

            if ($tens < 20) {
                $tens = ($tens ? ' ' . $list1[$tens] . ' ' : '');
            } else {
                $tens = (int) ($tens / 10);
                $tens = ' ' . $list2[$tens] . ' ';
                $singles = (int) ($num_part % 10);
                $singles = ' ' . $list1[$singles] . ' ';
            }
            $words[] = $hundreds . $tens . $singles . (($levels && (int) ($num_part)) ? ' ' . $list3[$levels] . ' ' : '');
        }
        $commas = count($words);
        if ($commas > 1) {
            $commas = $commas - 1;
        }

        $words  = implode(', ', $words);

        $words  = trim(str_replace(' ,', ',', ucwords($words)), ', ');
        if ($commas) {
            $words  = str_replace(',', ' and', $words);
        }

        return $words;
    } else if (!((int) $num)) {
        return 'Zero';
    }
    return '';
}

$word = numberToWord(120);
echo "<h4>120: " . $word . "</h4>";

$word = numberToWord(4500);
echo "<h4>4500: " . $word . "</h4>";

$word = numberToWord(58010);
echo "<h4>58010: " . $word . "</h4>";

Here,

numberToWord() is a user defined function in which we are passing numbers and returns the words format of it.

Application Testing

Now,

Open project into browser.

URL: http://localhost/number-to-word/index.php

We hope this article helped you to learn How To Convert Number To Words in PHP Example Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.