All of these answers will work well, but if you're looking for a reusable way, you can always externalise it:
function get_plural($value, $singular, $plural){
if($value == 1){
return $singular;
} else {
return $plural;
}
}
$value = 0;
echo get_plural($value, 'user', 'users');
$value = 3;
echo get_plural($value, 'user', 'users');
$value = 1;
echo get_plural($value, 'user', 'users');
// And with other words
$value = 5;
echo get_plural($value, 'foot', 'feet');
$value = 1;
echo get_plural($value, 'car', 'cars');
Or, if you want it to be even more automated, you can set it up to only need the $plural
variable set when it is an alternate word (eg: foot/feet):
function get_plural($value, $singular, $plural = NULL){
if($value == 1){
return $singular;
} else {
if(!isset($plural)){
$plural = $singular.'s';
}
return $plural;
}
}
echo get_plural(4, 'car'); // Outputs 'cars'
echo get_plural(4, 'foot'); // Outputs 'foots'
echo get_plural(4, 'foot', 'feet'); // Outputs 'feet'