Display numbers with ordinal suffix in PHP

Display numbers with ordinal suffix in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Display numbers with ordinal suffix in PHP

Displaying raw integers often feels incomplete in user-facing interfaces. Seeing "1st", "2nd", or "150th" provides immediate context that a bare number lacks, whether you are ranking items in a list or showing floor numbers in a building directory. While this seems trivial, the logic required to determine the correct suffix—st, nd, rd, or th—is surprisingly nuanced. A naive approach often fails at specific edge cases, particularly with numbers ending in 11 through 13.

The standard heuristic relies on the last digit of the number. If a number ends in 1, it typically takes "st" (e.g., 21st); if it ends in 2, it takes "nd" (e.g., 22nd); and if it ends in 3, it takes "rd" (e.g., 23rd). However, this rule breaks down completely for multiples of ten plus one, two, or three. Consider the number 11: despite ending in 1, it is "11th", not "11st". Similarly, 12 is "12th" and 13 is "13rd". This exception exists because these specific numbers fall within the teens, where the suffix logic resets to "th".

To handle this correctly in PHP, you must check the last two digits of the number before applying the single-digit rule. The most robust pattern involves checking if the number ends in 11, 12, or 13 first. If it does, you force the suffix to "th". Only after ruling out these exceptions do you inspect the final digit to assign st, nd, or rd.

Here is a function that implements this logic:

<?php

function getOrdinalSuffix(int $number): string
{
    // Handle the special case for numbers ending in 11-13
    if (($number % 100) >= 11 && ($number % 100) <= 13) {
        return 'th';
    }

    // Determine suffix based on the last digit
    switch ($number % 10) {
        case 1:
            return 'st';
        case 2:
            return 'nd';
        case 3:
            return 'rd';
        default:
            return 'th';
    }
}

// Usage examples
echo getOrdinalSuffix(1) . "st\n";     // Output: st
echo getOrdinalSuffix(2) . "nd\n";     // Output: nd
echo getOrdinalSuffix(3) . "rd\n";     // Output: rd
echo getOrdinalSuffix(11) . "th\n";    // Output: th (Corrected from st)
echo getOrdinalSuffix(150) . "th\n";   // Output: th

The code uses the modulo operator (%) to isolate the relevant digits. The first check isolates the last two digits by calculating $number % 100. If that result is between 11 and 13 inclusive, we skip the single-digit logic entirely. The switch statement then efficiently maps the remaining cases. This approach ensures that numbers like 21 correctly return 'st' while 11 returns 'th'.

In a Laravel application, you might integrate this into a helper class or a trait if you frequently need to format lists of ranked items. You could also create a macro for the Number facade, though keeping it as a standalone function is often cleaner for simple utilities. If you are building a complex ranking system where performance is critical across millions of records, avoid calling this function inside tight loops without caching the result, although the computational cost here is negligible compared to database queries.

One common pitfall developers encounter is hardcoding the suffixes into a single switch statement without the modulo check for the teens. This results in incorrect output for numbers like 11 or 12, which undermines the user experience. Always validate the double-digit exception before applying the single-digit rule.

For more complex localization needs where ordinal rules differ by language (such as French or German), this logic would need to be abstracted further to accept a locale parameter and apply specific linguistic rules. However, for standard English output, the modulo-based approach described above remains the industry standard for balancing readability with correctness.

Our network