This is a discussion on "Question about multiple "if" conditions" within the PHP Forum section. This forum, and the thread "Question about multiple "if" conditions are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Question about multiple "if" conditions
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
||||
|
Question about multiple "if" conditions
This has bothered me for a long time. I'm hoping it's a really dumb question.
When you have a string of conditions in an 'if' clause and one side of the conditional expression is the same in each condition, is there any shortcut to avoid writing out every expression in full. E.g. instead of
|
|
|
|
|||
|
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)) { ... |
![]() |
| Tags |
| php |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Creating a "tag" system to find relevant "related" pages | MrQuestions | PHP Forum | 3 | Mar 20th, 2008 23:06 |
| [SOLVED] Show "Image" Depends On User "Status"? | Monie | Classic ASP | 6 | Oct 16th, 2007 01:22 |
| ? IS "meta name="robots" content="?" necessary in pages ? | Love2Java | Starting Out | 6 | Aug 8th, 2007 13:48 |
| Free "Terms and Conditions" | nate2099 | Website Planning | 2 | Aug 1st, 2007 16:16 |
| window.opener.document["nameForm"].getElementById("someid").value; doesnt work | drpompeii | JavaScript Forum | 0 | Feb 17th, 2007 23:09 |