You want to correctly pluralize words based on the value of a variable. For example, you are returning text that depends on the number of matches found by a search.
Use a conditional expression.
<html>
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
function pc_may_pluralize($singular_word, $amount_of) {
// array of special plurals
$plurals = array('fish' =>'fish', 'person' => 'people');
// only one
if (1 == $amount_of) {
return $singular_word;
}
// more than one, special plural
// use isset() to find a key whose associated value is anything but null
if (isset($plurals[$singular_word])) {
return $plurals[$singular_word];
}
// more than one, standard plural: add 's' to end of word
return $singular_word.'s';
}
$number_of_fish = 1;
echo "I ate $number_of_fish " . pc_may_pluralize('fish', $number_of_fish).'.<br />';
$number_of_fish = 5;
echo "I ate $number_of_fish " . pc_may_pluralize('fish', $number_of_fish).'.<br />';
$number_of_people = 5;
echo "There are $number_of_people " . pc_may_pluralize('person', $number_of_people).'.<br />';
$number_of_cars = 20;
echo "There are $number_of_cars " . pc_may_pluralize('car', $number_of_cars).'.';
?>
</p>
</body>
</html>
|