PHP Plural; Pluralize Words in PHP; Plural Word, plural()

function plural($count, $singular, $plural = 's') {
    if ($plural === 's') {
        $plural = $singular . $plural;
    }
    return ($count == 1 ? $singular : $plural);
}
Example:
$amount = 5;
echo 'And then I found ' . $amount . ' ' . plural($amount, 'dollar') . "\n";
 
$count = rand(1, 2);
echo 'Roll the ' . plural($count, 'die', 'dice') . "\n";
Output:
And then I found 5 dollars
Roll the die
Did this help you? Please let me know with a comment. Thank you
Posted .

2 comments (RSS)

  1. anonymous

    Child plural is children not childs.

    Party plural is Parties not Partys.

  2. anonymous

    for ($i = 0; $i <= 3; $i++) {

        echo $i . ' ' . plural($i, 'child', 'children') . "\n";

    }

    0 children

    1 child

    2 children

    3 children

Leave a comment