View Single Post
  #2 (permalink)  
Old May 22nd, 2007, 07:14
grahame grahame is offline
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Question about multiple "if" conditions

The short answer is "no - you must write it all out if you're using the != operator". In Perl 6 (different language) you WILL have a shortened syntax for this sort of thing but that's highly unusual.

In php you could write
if (!ereg ('^(Sally|Bob|Hermione)$,$name)) { ....
although that would be slighly less efficient at run time.

I do worry about hardcoding data such as people's names into a program. When Hermione decides she now wants to be called "Squirrel", or when Chloe joins the team, you're back to the code! Your code would be more maintainable if you had an array of names in a separate file. You could compromise with something like:

$people = array("Hermione","Bob","Sally");
if (in_array($name,$people)) { ...
Reply With Quote