470,827 Members | 1,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,827 developers. It's quick & easy.

singular or plural output string

This might seem like a trivial thing, but has anyone has come up with a
better way of outputting a singular vs. plural string?

For example:

// default plural label
$string = "appointments";
// singular label
if (mysql_num_rows($results) == 1) $string = "appointment";
print mysql_num_rows($results) . $string;

$results always contain 1 or more records so "0 appointments" is not
applicable.

Is there a way to do this in one line (perhaps with regular expressions)
without resorting to something lame like "1 appointment(s)"?

Again, no biggie and the above syntax is tried and true, but it's always
nice to learn a new trick now and then.

Thanks.
Jan 14 '06 #1
7 12905
On Fri, 13 Jan 2006 16:24:03 -0800, "Bosconian" <bo*******@planetx.com> wrote:
This might seem like a trivial thing, but has anyone has come up with a
better way of outputting a singular vs. plural string?

$results always contain 1 or more records so "0 appointments" is not
applicable.

Is there a way to do this in one line (perhaps with regular expressions)
without resorting to something lame like "1 appointment(s)"?

Again, no biggie and the above syntax is tried and true, but it's always
nice to learn a new trick now and then.


I tend to use something like:

print $count . ' noun' . ($count == 1 ? '' : 's')

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jan 14 '06 #2
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:4k********************************@4ax.com...
On Fri, 13 Jan 2006 16:24:03 -0800, "Bosconian" <bo*******@planetx.com> wrote:
This might seem like a trivial thing, but has anyone has come up with a
better way of outputting a singular vs. plural string?

$results always contain 1 or more records so "0 appointments" is not
applicable.

Is there a way to do this in one line (perhaps with regular expressions)
without resorting to something lame like "1 appointment(s)"?

Again, no biggie and the above syntax is tried and true, but it's always
nice to learn a new trick now and then.


I tend to use something like:

print $count . ' noun' . ($count == 1 ? '' : 's')

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool


That works nicely--thanks!
Jan 14 '06 #3
Bosconian wrote:
This might seem like a trivial thing, but has anyone has come up with a
better way of outputting a singular vs. plural string?

For example:

// default plural label
$string = "appointments";
// singular label
if (mysql_num_rows($results) == 1) $string = "appointment";
print mysql_num_rows($results) . $string;

$results always contain 1 or more records so "0 appointments" is not
applicable.

Is there a way to do this in one line (perhaps with regular expressions)
without resorting to something lame like "1 appointment(s)"?

Again, no biggie and the above syntax is tried and true, but it's always
nice to learn a new trick now and then.

Thanks.


Well, you can always put that code into a function in its own php file
and then just include it in your php file which will use it... Then you
could call it on one line each time you needed it. For example:

include('printplural.php');
....
print_plural($mystring, $db_result);
....
Jan 15 '06 #4
<jo*********@sbcglobal.net> wrote in message
news:f9*****************@newssvr21.news.prodigy.co m...
Bosconian wrote:
This might seem like a trivial thing, but has anyone has come up with a
better way of outputting a singular vs. plural string?

For example:

// default plural label
$string = "appointments";
// singular label
if (mysql_num_rows($results) == 1) $string = "appointment";
print mysql_num_rows($results) . $string;

$results always contain 1 or more records so "0 appointments" is not
applicable.

Is there a way to do this in one line (perhaps with regular expressions)
without resorting to something lame like "1 appointment(s)"?

Again, no biggie and the above syntax is tried and true, but it's always
nice to learn a new trick now and then.

Thanks.


Well, you can always put that code into a function in its own php file
and then just include it in your php file which will use it... Then you
could call it on one line each time you needed it. For example:

include('printplural.php');
...
print_plural($mystring, $db_result);
...


I wrap reusable code in functions all the time, but in this case it's not
needed.
Jan 16 '06 #5
"Bosconian" <bo*******@planetx.com> wrote in message
news:_Z******************************@comcast.com. ..
<jo*********@sbcglobal.net> wrote in message
news:f9*****************@newssvr21.news.prodigy.co m...
Bosconian wrote:
This might seem like a trivial thing, but has anyone has come up with a better way of outputting a singular vs. plural string?

For example:

// default plural label
$string = "appointments";
// singular label
if (mysql_num_rows($results) == 1) $string = "appointment";
print mysql_num_rows($results) . $string;

$results always contain 1 or more records so "0 appointments" is not
applicable.

Is there a way to do this in one line (perhaps with regular expressions) without resorting to something lame like "1 appointment(s)"?

Again, no biggie and the above syntax is tried and true, but it's always nice to learn a new trick now and then.

Thanks.


Well, you can always put that code into a function in its own php file
and then just include it in your php file which will use it... Then you
could call it on one line each time you needed it. For example:

include('printplural.php');
...
print_plural($mystring, $db_result);
...


I wrap reusable code in functions all the time, but in this case it's not
needed.


On second thought, breaking this out as a function proves useful:

function print_plural($temp, $count) {
return $count . ' ' . $temp . ($count == 1 ? '' : 's');
}

print print_plural('appointment', mysql_num_rows($result));
Jan 16 '06 #6
Bosconian said the following on 16/01/2006 21:03:
<jo*********@sbcglobal.net> wrote in message
news:f9*****************@newssvr21.news.prodigy.co m...
Bosconian wrote:
This might seem like a trivial thing, but has anyone has come up with
a better way of outputting a singular vs. plural string?
Well, you can always put that code into a function in its own php file
and then just include it in your php file which will use it... Then you
could call it on one line each time you needed it. For example:

include('printplural.php');
...
print_plural($mystring, $db_result);
...


On second thought, breaking this out as a function proves useful:

function print_plural($temp, $count) {
return $count . ' ' . $temp . ($count == 1 ? '' : 's');
}

print print_plural('appointment', mysql_num_rows($result));


Of course, beware of non-standard pluralisation, e.g. "children",
"geese", "bacteria", "fish", "mice", "oxen", etc.
--
Oli
Jan 17 '06 #7
Oli Filth (ca***@olifilth.co.uk) wrote:
: Bosconian said the following on 16/01/2006 21:03:
: >> <jo*********@sbcglobal.net> wrote in message
: >> news:f9*****************@newssvr21.news.prodigy.co m...
: >>> Bosconian wrote:
: >>>> This might seem like a trivial thing, but has anyone has come up with
: >>>> a better way of outputting a singular vs. plural string?
: >>>>
: >>>>
: >>> Well, you can always put that code into a function in its own php file
: >>> and then just include it in your php file which will use it... Then you
: >>> could call it on one line each time you needed it. For example:
: >>>
: >>> include('printplural.php');
: >>> ...
: >>> print_plural($mystring, $db_result);
: >>> ...
: >
: > On second thought, breaking this out as a function proves useful:
: >
: > function print_plural($temp, $count) {
: > return $count . ' ' . $temp . ($count == 1 ? '' : 's');
: > }
: >
: > print print_plural('appointment', mysql_num_rows($result));
: >

: Of course, beware of non-standard pluralisation, e.g. "children",
: "geese", "bacteria", "fish", "mice", "oxen", etc.

don't get too tricky (untested)

function plural( $count, $word, $words )
{
if ($count > 1)) return $words;
else return $word;
}

e.g.

print "the ".plural($count,"customer","customers")
print "the ".plural($cooked,"goose" ,"geese")
Or use a lookup table to convert singles to plurals
# define array $plurals ahead of time with all the
# words that pluralizing

function plural( $count, $word )
{
if ($count >1) return $plurals[$word];
else return $word;

Jan 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Trevor Best | last post: by
2 posts views Thread by Steve - DND | last post: by
reply views Thread by Slypig | last post: by
31 posts views Thread by Bill Norton | last post: by
4 posts views Thread by guate911 | last post: by
1 post views Thread by tryce430 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.