Ever see those annoying sentences that have incorrect grammar based on integer variables? Well, I always write a function to correctly display the word in question and here it is!
PHP Example :
<?php
/*
This snippet comes with no warranty, express nor implied, and hence,
no guarantees are provided as to how it may run with your setup.
YMMV...
--Vorlin 2/17/2005
Purpose: provide correct ending for words when an int variable is
given. If 1, return nothing, if 0 or greater than 1, return "s".
It also corrects "is" and "are", based on variable type. Only
works on positve values.
0 - will return "is" or "are" based on variable
1 - will return "" or "s" for endings on words based on variable
*/
function correct_show($type, $variable) {
switch ($type) {
case 0:
return ($variable == 1 ? "is" : "are");
break;
case 1:
return ($variable == 1 ? "" : "s");
break;
} // end switch
}
?>
Example use:
PHP Example :
<?
$count = 1;
printf("There %s %d way%s to skin a cat.\n", correct_show(0, $count), $count, correct_show(1, $count));
?>