Skip to content

Instantly share code, notes, and snippets.

@scottnunemacher
Last active August 8, 2022 18:11
Show Gist options
  • Save scottnunemacher/7b6a16d3ecf64a8ca12dd88803698df1 to your computer and use it in GitHub Desktop.
Save scottnunemacher/7b6a16d3ecf64a8ca12dd88803698df1 to your computer and use it in GitHub Desktop.
PHP - Test a value and return plural if needed.
<?php
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'
/* Can supply plural term. */
echo get_plural(8, 'goose', 'geese'); // Outputs 'geese'
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment