Page 1 of 1

Or Statement inside if

Posted: Sat Mar 14, 2009 4:30 pm
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.

Re: Or Statement inside if

Posted: Sat Mar 14, 2009 4:32 pm
by panic!
you need to write
if ($var != '12345' || $var= '67890') {

}

Re: Or Statement inside if

Posted: Sat Mar 14, 2009 4:33 pm
by Benjamin
It's counter intuitive, you need to use the AND operator.

Code: Select all

 
if ($var != '12345' && $var != '67890') {
 
}
 

Re: Or Statement inside if

Posted: Sat Mar 14, 2009 4:37 pm
by LanceEh
Could of swore I tried one of those, anyways, thanks, I will give it a shot!

Many thanks.

Re: Or Statement inside if

Posted: Sat Mar 14, 2009 4:38 pm
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.

Re: Or Statement inside if

Posted: Sat Mar 14, 2009 4:40 pm
by Benjamin
At that point I would use an array.

Code: Select all

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