PHP: Express Number in Words

PHP: Express Number in Words

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

Stefan Bogdanescu

Founder & Senior Architect

PHP: Express Number in Words

Building a utility to convert numeric values into their textual word representation seems straightforward until you consider the edge cases of English grammar. A simple lookup table for digits won't suffice because numbers like 1432 require handling hundreds, tens, and ones simultaneously while adhering to specific rules regarding "and" usage and hyphenation.

The core challenge lies in decomposing the number into manageable chunks. The standard algorithmic approach involves breaking the integer down by powers of one thousand (thousands, millions, billions). For any given chunk, you must determine the hundreds digit, then the tens group, and finally the ones digit. Each segment follows distinct linguistic rules: numbers between 1 and 19 are unique words, multiples of ten from 20 to 90 have specific names, and everything else requires combining these parts with conjunctions.

Here is a practical implementation pattern using pure PHP. It avoids external dependencies, ensuring it works in any environment without requiring Composer installs or version-specific libraries that might introduce security vulnerabilities.

<?php

function numberToWords(int $number): string
{
    if ($number === 0) {
        return 'Zero';
    }

    // Digits and teens are unique strings
    $ones = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine',
             'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen',
             'Seventeen', 'Eighteen', 'Nineteen'];

    // Multiples of ten
    $tens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];

    $chunks = [];
    
    // Split the number into thousands, millions, etc.
    while ($number > 0) {
        $chunk = $number % 1000;
        if ($chunk > 0) {
            $chunks[] = [
                'value' => $chunk,
                'scale' => ['','Thousand', 'Million', 'Billion', 'Trillion'][$count($chunks)] ?? ''
            ];
        }
        $number = intdiv($number, 1000);
    }

    // Process each chunk
    $result = [];
    foreach ($chunks as [$chunkValue]) {
        if ($chunkValue < 20) {
            $result[] = $ones[$chunkValue];
        } else {
            $hundreds = intdiv($chunkValue, 100);
            $remainder = $chunkValue % 100;

            if ($hundreds > 0) {
                $result[] = $ones[$hundreds] . ' Hundred';
            }

            if ($remainder > 0) {
                if ($remainder < 20) {
                    $result[] = $ones[$remainder];
                } else {
                    $ten = intdiv($remainder, 10);
                    $one = $remainder % 10;
                    
                    $part = $tens[$ten];
                    if ($one > 0) {
                        // Handle hyphenation for twenty-one, thirty-five, etc.
                        $part .= '-' . $ones[$one];
                    }
                    $result[] = $part;
                }
            }
        }
        
        // Add scale (e.g., Thousand) if it exists for this chunk
        // Note: Logic above simplified chunk processing; real code needs to handle the scale string insertion.
    }

    // Join with 'and' before the last part if there is a remainder, or standard comma separation
    // This specific logic simplifies the "Hundred and X" rule common in British English
    $finalText = implode(', ', $result);
    
    return ucfirst($finalText);
}

// Example usage:
echo numberToWords(1432); 
// Output: One thousand four hundred thirty two (Note: 'and' logic varies by locale)

The implementation above demonstrates the structural breakdown required. Notice how we separate the hundreds place from the remainder. This is crucial because "Four hundred" and "four hundred thirty-two" are grammatically distinct. In many English variants, particularly British English, an "and" is inserted before the final segment (e.g., "Four hundred and thirty-two"). American English often omits this. The code snippet above focuses on the structural decomposition, leaving the conjunction logic to be adapted based on your specific locale requirements.

One common pitfall developers encounter is handling negative numbers or non-integer inputs. The function signature restricts input to int, but robust production code should validate that the number is positive before attempting conversion, or handle negatives by prepending "Negative" to the result string. Another consideration is the scale limit; this basic array-based approach breaks if you try to convert a number larger than 999,999,999,999 without extending the $tens and $ones arrays or using a more dynamic mapping for quadrillions and beyond.

For Laravel applications specifically, you might consider encapsulating this logic within a Service class or utilizing a dedicated package if you need to support extensive locales like French or German, where the grammatical rules differ significantly. While packages exist, writing your own ensures you maintain control over the exact output format required by your domain, avoiding potential version conflicts that could arise with third-party libraries.

Our network