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"