Plural function

I know it seems basic, but I wrote this a while ago and use it in many many applications I’ve written. I’m surprised alot of languages don’t have something this simple in a small, native function…

Takes three arguments: number, singular and plural.

Basically, if number is plural (!= 1), return plural, else return singular, as per the following PHP code:

function plural($num, $singular, $plural) {
	if ($num != 1) return $plural;
	return $singular;
}

Useful when writing copy that needs to read correctly, eg: You have 0 credits; You have 1 credit; You have 2 credits.

You could write:

print "You have " . plural($num, "$num credit", "$num credits");

*shrug* I like it.

This entry was posted in Coding, Language, PHP. Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>