Or Statement inside if

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
User avatar
LanceEh
Forum Commoner
Posts: 46
Joined: Tue Feb 17, 2009 11:53 am
Location: Florida

Or Statement inside if

Post by LanceEh »

I've tried everything I can think of to get this working.

This seems logical, but doesn't work (only seems to register on the first number being checked)

Code: Select all

if ($var != '12345' || '67890') {
 
}
Thanks.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Or Statement inside if

Post by panic! »

you need to write
if ($var != '12345' || $var= '67890') {

}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Or Statement inside if

Post by Benjamin »

It's counter intuitive, you need to use the AND operator.

Code: Select all

 
if ($var != '12345' && $var != '67890') {
 
}
 
User avatar
LanceEh
Forum Commoner
Posts: 46
Joined: Tue Feb 17, 2009 11:53 am
Location: Florida

Re: Or Statement inside if

Post by LanceEh »

Could of swore I tried one of those, anyways, thanks, I will give it a shot!

Many thanks.
User avatar
LanceEh
Forum Commoner
Posts: 46
Joined: Tue Feb 17, 2009 11:53 am
Location: Florida

Re: Or Statement inside if

Post by LanceEh »

If I'm wanting to do three or more would it be logical to

Code: Select all

if ($var != '12345' && $var != '67890' && $var !='09876')
?
By the way, heck of a fast response.

Thanks.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Or Statement inside if

Post by Benjamin »

At that point I would use an array.

Code: Select all

 
$foo = array(1, 2, 3);
 
if (!in_array($value, $foo)) {
 
}
 
Post Reply