Question about multiple "if" conditions

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.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old May 21st, 2007, 22:05
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
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
PHP: Select all

if(($name!='Bob')&&($name!='Sally')&&($name!='Hermione')) { 

is there some way to write
PHP: Select all

if($name!=('Sally'||'Bob'||'Hermione')) { 

I feel like there must be, but my experiments have failed, as has Google. Hopefully this is just a question of getting the various brackets and operators in right order.
Reply With Quote

  #2 (permalink)  
Old May 22nd, 2007, 07:14
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
  #3 (permalink)  
Old May 22nd, 2007, 13:21
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Question about multiple "if" conditions

Thanks so much Graham. When someone writes a PHP book and thinks to include all the little things that you can and can't do (e.g. I am amazed at all the unnecessary concatenation when people echo/print variables, especially in queries) . . .

The people's names were just for illustration (I'm really worried about $_POST variables at the moment) but the idea of putting the values in an array is actually a workaround for the original question and makes for a nice module or class method.

I need to look up a few techniques re: user-defined functions, but I think I actually will do a little module of some flavor. Maybe I'll get ambitious and do a class for $PHPSELF forms.

If I do, I'll post it here for your amusement.
Reply With Quote
Reply

Tags
php

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 07:08.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43