Skip to content

Instantly share code, notes, and snippets.

@d1i1m1o1n
Created February 13, 2016 09:40
Show Gist options
  • Save d1i1m1o1n/0d060f76a3fd2e869252 to your computer and use it in GitHub Desktop.
Save d1i1m1o1n/0d060f76a3fd2e869252 to your computer and use it in GitHub Desktop.
Вывести правильное окончание слова "товар" в зависимости от количества добавленных товаров в корзину (например, 1 товар, 3 товара, 5 товаров)
<?
function getNumEnding($number, $endingArray) {
$number = $number % 100;
if ($number>=11 && $number<=19) {
$ending=$endingArray[2];
} else {
$i = $number % 10;
switch ($i) {
case (1): $ending = $endingArray[0]; break;
case (2): case (3): case (4): $ending = $endingArray[1]; break;
default: $ending=$endingArray[2];
}
}
return $ending;
}
?>
<?=$num?> <?=getNumEnding($num, Array("товар", "товара", "товаров"))?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment