Blog

Give me a call...    (619) 793-4645

SEARCH

CONNECT WITH ME

  • Facebook 
  • Twitter 
  • LinkedIn

About Me

I'm a freelance web expert who loves delivering big results to small businesses. I specialize in website design, e-mail marketing, and internet advertising.

Happy Clients

Very professional, helpful, efficient, knowledgeable, and most importantly - they understand marketing. Matt made a confusing process easy and painless.

Claudia
Marathon Triad

Matt has dedicated his time and creativity to making the Tustin Garden Club website both functional and beautiful! Matt has responded to every request for modification and periodic updates willingly and expeditiously. I would highly recommend GoldVine to anyone for web design and maintenance.

Anita Zantos
Tustin Garden Club

PHP Function that Chooses a Singular or Plural Word based on an Input Number

Posted in: Blog, PHP, Tutorials by Matt Goldman on November 11, 2010


The other day, I needed a PHP function to display a word either singular or plural based on a number referring to that word. For example, this could be used on a blog post which may have 0 comments, 1 comment, or 2 comments. Feel free to use this in your projects, or modify it as needed.

function word_choice($number, $singular, $plural) {
    if($number == 0 OR $number >= 2) {
        return $number . " " . $plural;
    }else{
        return $number . " " . $singular;
    }
}

//Implementation
$number = 32;

echo word_choice($number, 'comment', 'comments');
//This will show "32 comments"

$number = 1;

echo word_choice($number, 'comment', 'comments');
//This will show "1 comment"