Robb Kerr wrote:
> I need some help with a looping syntax. In english, "a" is used before
> words that begin with consonants - "an" is used before words that
> start with vowels.

You are probably already aware of this, but that isn't strictly correct.
The rule for deciding between "a" and "an" is based not on the presence of
consonants and vowels, but on the presence of consonant and vowel _sounds_.
Most of the time these are the same, but sometimes they are not, such as in
the phrases "an honorable agreement" or "a useful idea".  This distinction
may not matter for your program, but I thought I'd mention it just in case
you hadn't considered it.

> I'm trying to create a loop that checks this state
> and inserts the correct word in the echo line. Below is the sloppy
> version of what I'm trying to do...
[...]
> There's got to a cleaner way to do this.

Here's my version:

if (preg_match('/^[aeiou]/i', $word)) {
  echo 'an';
} else {
  echo 'a';
}

Mine is probably the least efficient approach of all those given thus far
from a pure performance standpoint, since I'm incurring the overhead of the
regex engine for something that can be accomplished without it, but IMHO it
is so much easier on the eyes that it's worth it.  Of course others may
disagree. :)

HTH

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to