Page 1 of 1
if != Value - How to add multiple values?
Posted: Wed Jun 24, 2009 4:02 pm
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.
Re: if != Value - How to add multiple values?
Posted: Wed Jun 24, 2009 4:17 pm
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
Re: if != Value - How to add multiple values?
Posted: Wed Jun 24, 2009 5:57 pm
by requinix
Rather than use a loop use
a function.
Code: Select all
if (in_array($value, array("130", "98", "176", "87", "77", "52")))
Re: if != Value - How to add multiple values?
Posted: Wed Jun 24, 2009 6:03 pm
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
Re: if != Value - How to add multiple values?
Posted: Wed Jun 24, 2009 7:04 pm
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.
Re: if != Value - How to add multiple values?
Posted: Thu Jun 25, 2009 6:58 am
by MirceaT
Thanx a lot, that did it!
Regards.