Skip to content

Instantly share code, notes, and snippets.

@kvalexandr
Last active November 29, 2015 00:24
Show Gist options
  • Save kvalexandr/2e79e1f16a72ecb3f5d1 to your computer and use it in GitHub Desktop.
Save kvalexandr/2e79e1f16a72ecb3f5d1 to your computer and use it in GitHub Desktop.
Plural numbers
<?php
function plural($number, $titles) {
$cases = array (2, 0, 1, 1, 1, 2);
return $titles[ ($number % 100 > 4 && $number % 100 < 20) ? 2 : $cases[min($number % 10, 5)] ];
}
@kvalexandr
Copy link
Author

// Пример:
$number = 10;
echo plural($number, array('комментарий', 'комментария', 'комментариев'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment