Simple PHP Pluralize

I often find myself writing a if statement when I need to pluralize a noun in PHP. I recently wrote a simple function that did the task for me. I know there are PHP Pluralize functions out there, but they are a bit overkill for my needs. I got this idea from the way Django templates handles pluralizing. I once heard someone say it can’t pluralize octopus. It can, but how often does that come up? My simple function here can also pluralize Octopus or any other word really.

Let’s say I want to echo the following:

There is 1 user.

There are 2 users.

Here was my hold way of doing things quick and dirty with no functions:

There <?=($num_users == 1) ? 'is' : 'are';?> <?=$num_users?> user<?($num_users != 1) echo 's';?>.

Now, let’s write a simple function to use:

function pluralize($num, $plural = 's', $single = '') {
    if ($num == 1) return $single; else return $plural;
}

Now I would write it like this:

There <?=pluralize($num_users, 'are', 'is')?> <?=$num_users?> user<?=pluralize($num_users)?>.

OK, I don’t know if saves me a whole lot of typing or not, but anyway, it makes it a bit easier in my mind.

Now, for the Octopus example:

1 Octopus has 8 legs.

7 Octopi have 56 legs.

<?=$num?> Octop<?=pluralize($num, 'i', 'us')?> ha<?=pluralize($num, 've', 's')?> <?=(8 * $num)?> leg<?=pluralize(8 * $num)?>.

(I thought I’d better pluralize "legs" in case we ever have 0.125 Octopi)

[Big nerdy cheesy grin!]

Related posts:

  1. Django: Login Form on Every Page Up to the point, when it has come to Django...

Related posts brought to you by Yet Another Related Posts Plugin.

April 13, 2009   Posted in: Programming & Internet

14 Responses

  1. AJ - April 13, 2009

    Are you trying to promote PHP on Django’s website? Is this your intention? Who are you working for?
    But seriously, this post showed up in the Django Community website.
    Nice function… I think I’ll use it when I need to do something similar with PHP.

  2. Dustin - April 13, 2009

    haha, no. I swear that was totally unintentional. Basically any post on my blog here that contains the word Django is included in the Django feed. I forgot about that. I was just giving props where props were due!

  3. é¢ç» - April 13, 2009

    This script is useful!
    Thanks for sharing.

  4. Da brush from photoshop brushes - April 14, 2009

    wow this script is great, thank you ;)

  5. Jye from Web and graphic design services - May 5, 2009

    I use the same, similar, function to complete a task like this.

  6. Anonymous - May 5, 2009

    Obviously none of you (poster and commenters) heard of internationalization before, do you? This method is still quite “large” (return ($num==1)?$single:$plural;). Any benefit is lost as soon as one requires to support localizable web sites and guess you don’t need to leave the States for this … I’d appreciate to see a pluralizer as used in Rails instead.

  7. Dustin - May 5, 2009

    I’ve heard of internationalization… do I care? NO. I figured this was an arrogant Rails developer comment just from the tone. Precisely why I opted to learn Django instead. I can’t stand arrogant Rails mentality – too much like the arrogant Mac mentality.

    I don’t know how you can say a one liner function is quite “large”. Have you seen Rails’ pluralize function?

  8. Shaz from Photoshop Tutorials - May 28, 2009

    Im glad im more of a front end developer using actionscript 3 with flex i dont usually end up getting involved with these backend tiffs betweent the rails and the hp’s. Nice coding and yes really nerdy so you have my vote :)

  9. Bill from PHP code snippets forum - June 5, 2009

    Beautifully simple, Davis. I’ve tended to formulate my output so as to not make it as big a deal if I have the plural version or not (“Current users: X” etc), but I might rip you off now. :)

  10. Susan - June 12, 2009

    Brilliant. Sometimes keeping it simple is the best. I’m getting tired of bloated code.

  11. Emma Millward - September 20, 2009

    Dustin – I just wanted to post you a big thank you for providing such useful code I was hoping to PHP Pluralize functions but found some code very complex, I was looking a Paul’s Osman’s website (based on your post) in regards to the Rails implementation – thankfully with with both yours and Paul’s suggestion contribution was able to get the code working.

    Many thanks again, Emma

  12. Colon Cleansing - October 26, 2009

    Any benefit is lost as soon as one requires to support localizable web sites and guess you don’t need to leave the States for this …

  13. Michael Hodgins - November 20, 2009

    Hi. I didn’t use this script but it provided inspiration, so I thought I’d post a few pointers of my own. Don’t take these as criticisms.

    First, php short tags aren’t a good idea (I think they’re deprecated in PHP6) as they make the code harder to move from server to server; you should use ” and ‘ instead.

    Also, the two statements ‘if ($num == 1) return $single; else return $plural; ‘ can be tidied up by using the ternary operator: ‘return $num == 1 ? $single : $plural;’. Much nicer than that semi-colon in the middle of the line.

  14. Michael Hodgins - November 20, 2009

    Ooops, the server filtered my php tags. Fair enough I suppose.

Leave a Reply