5 way(s) to pluralize

Programmers are lazy or maybe they just can’t count. More and more I see sites out there with dumb things like “1 page(s)” or worse just “1 pages”. I could find lots of Yahoo pages with this kind of stuff, but that’s beside the point – I’m providing help for lazy programmers who can’t count.

The rule is simple. Words are plural for every value except 1. But now you don’t even have to remember that. Just cut and paste one of the functions below.

To use any of them, call them with the count of things and then the singular form of the word and the plural form. For the truly lazy, if you can pluralize your word by just appending an “s” to the end, the last argument is optional. But don’t do this with words like “quantity”, duh.

PHP Example:

I know 5 lazy < ?php echo pluralize(5, 'programmer', 'programmers'); ?>.

I know 5 lazy < ?php echo pluralize(5, 'programmer'); ?>.

Their < ?php echo pluralize(1, 'mommy', 'mommies') ?> didn't teach them to count.

Spits out:

I know 5 lazy programmers.

I know 5 lazy programmers.

Their mommy didn't teach them to count.

Here’s the code to cut ‘n paste:


function pluralize($count, $singular, $plural = false)
   if (!$plural) $plural = $singular . 's';

  return ($count == 1 ? $singular : $plural) ;

I’m surprised this isn’t already Yet Another PHP Function


sub pluralize()
   my ($count, $singular, $plural) = @_;

   $plural = $singular . 's' if (!$plural) ;

  return $singular if ($count == 1);
  return $plural; 

I’m sure someone will come along and rewrite this in one illegible line. Of course there’s already a CPAN module for this if you’re afraid of writing any code yourself.


public static String pluralize(int count, String singular)
   return pluralize(count, singular, singular.concat('s'));

public static String pluralize(int count, String singular, String plural)
  return (count == 1 ? singular : plural) 

Boy I wish Java had optional arguments.


String.prototype.pluralize = function(count, plural)
  if (plural == null)
    plural = this + 's';

  return (count == 1 ? this : plural) 

OO JavaScript is cool

Please contribute your favorite oddball language. It’s too early in the morning for me to write in C and I get hives anytime I look at Visual Basic.


  1. Oracle PL/SQL

    lcount IN NUMBER,
    lsingular IN VARCHAR2,
    lplural IN VARCHAR2
    lpluralstring VARCHAR2 (100);
    IF lcount = 1 THEN
    lpluralstring := lsingular;
    ELSIF lcount != 1 AND lplural IS NULL THEN
    lpluralstring := lsingular || ‘s’;
    ELSIF lcount != 1 AND lplural IS NOT NULL THEN
    lpluralstring := lplural;
    END IF;

    RETURN lpluralstring;


  2. Well, yeah, and mommy should be singular. Well, the engineer being discussed is named “Heather”. I’d also note that Mommy is a nice example of where adding ‘s’ just isn’t sufficient. So are “moose”,”hippopotamus”, and the singular of “data”*, but those are (another|different) issue(s).

    *(yes, I know that New Collegiate allows both “data” for singular and plural like they allow “hippopotamuses”, but the real versions are “datum” for singular and “hippopotami” for plural. Of those words, not the same thing. Dang, Where’s that dangling modifier CPAN module again?)

  3. In your example, if the number of programmers is one (the NY Times Style guide suggests spelling out the first nine cardinal and ordinal numbers in ordinary copy), the the last sentence should read: “His mommy didn’t teach him to count.”

  4. Here’s it in BASIC (sorry, it’s not a sub, whaddya gonna do about it?):

    10 INPUT “What is the number?”; count$
    20 INPUT “What is the singular?”; singular$
    30 INPUT “What is the plural? (leave blank for laziness)”; plural$
    40 IF plural$ = “” THEN GOTO 70
    50 IF count$ = 1 THEN GOTO 100
    60 PRINT “There are “; count$; plural$; “!”
    65 EXIT
    70 plural$ = singular$; “s”
    71 GOTO 50
    100 PRINT “There is “; count$; singular$; “!”
    110 EXIT

  5. Pingback: PHP-Scripts Blog

  6. I take it there is no easy (reliable) way in PHP to:
    a | Make singular words plural
    b | Make plural words singular

    For example, simply removing the S’s isn’t a good option since a word like grass would become gras. Adding an S is not ideal as fish becomes fishs… I guess if there is no vowel on the end, “es” could be added. Is there a function out there that anyone knows of that handles the various possibilities? If so, I would be very grateful if someone could email the link to me.

    Many thanks.

  7. I wrote this function today after not finding one in the PHP manual. I tried a few test cases but I’m no grammar expert so I may have missed some conditions. It’s really hard to tell how to pluralize words ending in ‘o’, so a lot of those might not work well. Also, for the words in the $irregulars array, the comparison is case sensitive, so you may have to add your own formatting in there. It’s not perfect, but it’s not bad for 10 mintues. If you see any more irregulars I am missing please post them in response.

    function pluralize($string)
    $irregulars = array(‘man’ => ‘men’, ‘woman’ => ‘women’, ‘fungus’ => ‘fungi’, ‘thief’ => ‘thieves’, ‘species’ => ‘species’,
    ‘medium’ => ‘media’,’person’ => ‘people’, ‘echo’ => ‘echoes’, ‘hero’ => ‘heroes’, ‘potato’ => ‘potatoes’,
    ‘veto’ => ‘vetoes’, ‘auto’ => ‘autos’, ‘memo’ => ‘memos’, ‘pimento’ => ‘pimentos’, ‘pro’ => ‘pros’);

    $es = array(‘s’, ‘z’, ‘ch’, ‘sh’, ‘x’);

    $last_letter = $string{strlen($string) – 1};

    if(array_key_exists($string, $irregulars))
    return $irregulars[$string];
    else if($last_letter == ‘y’)
    if($string{strlen($string) – 2} == ‘e’)
    return substr($string, 0, strlen($string) – 2) . ‘ies’;
    return substr($string, 0, strlen($string) – 1) . ‘ies’;
    else if(in_array(substr($string,0,-2),$es) || in_array($last_letter, $es))
    return $string . ‘es’;
    return $string . ‘s’;

  8. The same as yours, in one line, but yes a little less understandable:

    function pluralize($count, $singular, $plural = false)
    return ($count == 1 ? $singular : ($plural ? $plural : $singular . ‘s’)) ;

  9. # File actionpack/lib/action_view/helpers/text_helper.rb, line 183

    def pluralize(count, singular, plural = nil)
    “#{count || 0} ” + ((count == 1 || count == ‘1’) ? singular : (plural || singular.pluralize))

    Welcome to the Rails community. 🙂

  10. Love Rails 😉

    pluralize(1, ‘person’)
    # => 1 person

    pluralize(2, ‘person’)
    # => 2 people

    pluralize(3, ‘person’, ‘users’)
    # => 3 users

    pluralize(0, ‘person’)
    # => 0 people

  11. Bit of inline PHP, which also pluralizes in the case of 0 (your search returned 0 results).
    <p>your search returned <?php echo count($search_results);?> result<?php count($search_results)==0||count($search_results)>1?'s':'';?>

  12. My pluralize function in PHP.


    I know

    function pluralize( $count, $zero, $singular, $plural ) {
    if($count == 1) {
    return sprintf( $singular, $count );

    if($count > 1) {
    return sprintf( $plural, $count );

    return sprintf( $zero, $count );

  13. I know, it’s 8 years old, but your JS example will only work if you pass a null value for plural, not if you omit the value (which is more likely). So try:

    String.prototype.pluralize = function(count, plural)
    if (typeof plural !== "string")
    plural = this + 's';

    return (count == 1 ? this : plural)

  14. It’s fine to use ternary operator in a trivial case. However it will not work for multiple languages and it will be very difficult to translate the website in the future.

    To address this, I’ve created a small library that can be used to pluralize words in JavaScript. It transparently uses CLDR database for multiple locales and supports almost any language you would like to use. It’s API is very minimalistic and integration is extremely simple. I’ve called it — Numerous: https://github.com/betsol/numerous.

    Here’s a small introductory article on it: «How to pluralize any word in different languages using JavaScript?»: https://gist.github.com/slavafomin/f2e5259cab17d55af5d9fa4c2c2baa08.

    Feel free to use it in your project. I will also be glad to receive your feedback on it!

Leave a Reply

Your email address will not be published. Required fields are marked *