Re: How to: If($var != '#' OR '#')

From: Date: Wed, 20 Nov 2002 18:56:43 +0000
Subject: Re: How to: If($var != '#' OR '#')
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-125297@lists.php.net to get a copy of this message
--- Jason Wong <php-general@gremlins.biz> wrote: > On Thursday 21 November 2002 01:34, Ryan Gallagher wrote: > > if( ( $_GET['sc'] != 2 ) OR ( $_GET['sc'] != 8 ) ){ > > /* > > * Do Foo provided sc is anything but a 2 or 8 > > */ > > do foo; > > } > > > > Assuming of course that you meant to exclude cases of 2 or 8. > > The above would not have the intended effect. My boolean maths is > extremely rusty but I think it is equivalent to saying: > > if (!($_GET['sc'] == 2 AND $_GET['sc'] == 8)) > > Which means your code above is *always* TRUE, so do foo is always > executed! Your boolean math may be rusty, but you are correct. :-) I think, however, that he was simply focusing on a literal interpretation of the original poster's code without considering if it made sense. If you want to exclude 2 *and* exclude 8 (saying things aloud can sometimes reveal obvious logic problems), just write it like this: if($_GET["sc"] != 2 && $_GET["sc"] != 8) { ... } Chris

« previous php.general (#125297) next »