if != Value - How to add multiple values?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MirceaT
Forum Newbie
Posts: 3
Joined: Wed Jun 24, 2009 3:56 pm

if != Value - How to add multiple values?

Post by MirceaT »

Hello,
I am a pHp newby and I am trying to create a simple IF conditional statement. I got it to work with:

IF TOPIC_ID != 23 ...

I am trying to add multiple values, something like:

IF TOPIC_ID != 23, 104, 39, ...

but it does not work.

How can I add multiple values to this conditional statement?
Thank you.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: if != Value - How to add multiple values?

Post by Eric! »

Code: Select all

if( $a!=$b && $a!=$c) then {
  echo "A is not B AND A is not C<br>";
}
See logical operators http://www.w3schools.com/PHP/php_operators.asp

If you have a long list to check then you might want to use an array and a loop:

Code: Select all

$list=array("130","98","176"
                ,"87","77","52");
   foreach($list as $hit)
    {
        if($value==$hit) 
       {
          // found a match
          // do something
        }
    }
 
EDIT: added array option
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: if != Value - How to add multiple values?

Post by requinix »

Rather than use a loop use a function.

Code: Select all

if (in_array($value, array("130", "98", "176", "87", "77", "52")))
MirceaT
Forum Newbie
Posts: 3
Joined: Wed Jun 24, 2009 3:56 pm

Re: if != Value - How to add multiple values?

Post by MirceaT »

Thanx for the replys.
My actual code is:

$list = array("5817", "98", "176", "52");

The IF is:

if (!$user->data['is_registered'] && !$user->data['is_bot'] && topic_id != $list && ($ad_text = retrieve_ad( AD_TW, 1, ( $i + $start + 1 ) ) ))

$topic_id - should get any of the values from $list

How should I apply the loop or the in_array functions?
Sorry for the dumb questions but I am a newbye...

Thanx again
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: if != Value - How to add multiple values?

Post by Eric! »

Does anyone write code anymore or are there functions for everything?

Code: Select all

if (!$user->data['is_registered'] && !$user->data['is_bot'] && (!in_array($topic_id,$list)) && ($ad_text = retrieve_ad( AD_TW, 1, ( $i + $start + 1 ) ) ))
the !in_array($topic_id,$list) returns true if the value isn't in the list. If you want the opposite logic remove the ! I didn't really catch which way you wanted it.
MirceaT
Forum Newbie
Posts: 3
Joined: Wed Jun 24, 2009 3:56 pm

Re: if != Value - How to add multiple values?

Post by MirceaT »

Thanx a lot, that did it!
Regards.
Post Reply