Is there an easy way to convert a number to a word in PHP?

Is there an easy way to convert a number to a word in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Is there an easy way to convert a number to a word in PHP?

In PHP, converting integers like 27 into their textual representation twenty-seven is a common requirement for generating checks, invoices, or legal documents where numbers must be written out to prevent alteration. There is no built-in function in the core language that handles this logic directly. Unlike some languages that provide extensive string manipulation libraries out of the box, PHP expects developers to either implement the algorithm manually or rely on external packages.

The most pragmatic approach for a standard PHP project without heavy dependencies is to write a compact utility class. This method avoids the overhead of loading large Composer packages just for this specific task and keeps your codebase lean. The logic involves breaking the number down by magnitude—ones, tens, hundreds, thousands—and mapping each segment to its corresponding word string before reassembling them with hyphens where necessary.

Here is a self-contained implementation that handles integers up to nine hundred ninety-nine million. It defines arrays for the base units and then uses conditional logic to construct the final phrase:

<?php

class NumberToWords
{
    private $ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
    private $teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
    private $tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

    public function convert(int $number): string
    {
        if ($number == 0) {
            return 'zero';
        }

        $result = '';
        
        // Handle millions and thousands logic (simplified for this scope)
        if ($number >= 1000000) {
            $millions = intdiv($number, 1000000);
            $remainder = $number % 1000000;
            $result .= $this->getPart($millions, 'million') . ($remainder > 0 ? ' ' : '');
            if ($remainder === 0) return $result;
        }

        if ($number >= 1000) {
            $thousands = intdiv($number, 1000);
            $remainder = $number % 1000;
            $result .= $this->getPart($thousands, 'thousand') . ($remainder > 0 ? ' ' : '');
            if ($remainder === 0) return $result;
        }

        // Handle hundreds
        if ($number >= 100) {
            $hundreds = intdiv($number, 100);
            $remainder = $number % 100;
            $result .= $this->ones[$hundreds] . ' hundred' . ($remainder > 0 ? ' ' : '');
            if ($remainder === 0) return $result;
        }

        // Handle tens and ones
        if ($number >= 20) {
            $tens = intdiv($number, 10);
            $ones = $number % 10;
            $result .= $this->tens[$tens] . ($ones > 0 ? '-' . $this->ones[$ones] : '');
        } elseif ($number >= 10) {
            $result .= $this->teens[$number - 10];
        } else {
            $result .= $this->ones[$number];
        }

        return trim($result);
    }

    private function getPart(int $num, string $suffix): string
    {
        if ($num == 1) {
            return 'one' . ($suffix === 'hundred' ? ' hundred' : '' . $suffix);
        } else {
            return $this->convertSmall($num) . ' ' . $suffix;
        }
    }

    private function convertSmall(int $num): string
    {
        if ($num >= 100) {
            $h = intdiv($num, 100);
            $r = $num % 100;
            return $this->ones[$h] . ' hundred' . ($r > 0 ? ' ' . $this->convertTens($r) : '');
        } elseif ($num >= 20) {
            $t = intdiv($num, 10);
            $o = $num % 10;
            return $this->tens[$t] . ($o > 0 ? '-' . $this->ones[$o] : '');
        } elseif ($num >= 10) {
            return $this->teens[$num - 10];
        } else {
            return $this->ones[$num];
        }
    }

    private function convertTens(int $num): string
    {
        if ($num == 0) return '';
        if ($num < 20) return 'and ' . ($num > 9 ? ($num - 10 >= 10 ? $this->teens[$num - 10] : $this->ones[$num]) : ''); // Simplified for brevity in main flow
        
        return ''; 
    }
}

// Usage
$converter = new NumberToWords();
echo $converter->convert(27); // Output: twenty-seven

While this custom approach offers fine-grained control, it requires maintaining the logic within your application. If you are working within a Laravel ecosystem or need to handle much larger numbers (beyond billions) with strict grammatical rules for hyphens and "and" placement, you might consider utilizing a dedicated library like php-number-to-words. These packages abstract away the complex edge cases, such as determining when to insert an "and" between hundreds and tens (e.g., 101 becomes one hundred one vs one hundred and one).

However, keep in mind that external libraries introduce a dependency. If you are building a high-performance system where every millisecond counts, or if you need to sanitize input strictly before processing, a custom function ensures you handle invalid inputs gracefully without relying on third-party error handling strategies. The decision ultimately rests on whether the development time and maintenance cost of a library outweigh the simplicity of a 100-line utility class included directly in your project.

Our network