RE: [PHP] wierd if statement problem

From: Date: Wed, 21 May 2003 10:28:51 +0000
Subject: RE: [PHP] wierd if statement problem
Groups: php.general 
Request: Send a blank email to php-general+get-148397@lists.php.net to get a copy of this message
> -----Original Message----- > From: daniel@electroteque.org [mailto:daniel@electroteque.org] > Sent: 21 May 2003 07:50 > > other way round , if its not 1 or 2 i need to logout and > return with an > error Your problem here is your too-literal translation of your English description into PHP code. Unfortunately, "or" in English tends to have a much looser usage than the Boolean-logic OR used in most computer programming languages (well, all the ones I can think of, anyway). > if (($_SESSION['groupID']!==1) || ($_SESSION['groupID']!==2)) This test will always be TRUE -- because $_SESSION['groupID'] is always either not 1 or not 2 (if it's 1 then it's not 2; if it's 2 then it's not 1; and if it's anything else it's neither 1 nor 2 (why does that remind me of the Grand Old Duke of York?)). If you go back to your description of what you want, I think you'll agree that what you mean is "if it's not (1 or 2), I need to ..."; turning this into PHP gives you: if (!($_SESSION['groupID']===1) || ($_SESSION['groupID']===2))) By a useful rule of Boolean logic (deMorgan's laws) which says that !(a || b) is the same as (!a && !b), and !(a && b) is the same as (!a || !b), you can also express this as: if (($_SESSION['groupID']!==1) && ($_SESSION['groupID']!==2)) which could also be expressed in English as "if it's not 1 and it's not 2 ...". So either of these tests will do the job for you, and you can choose whichever one you find easier to understand. Cheers! Mike --------------------------------------------------------------------- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: m.ford@lmu.ac.uk Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

« previous php.general (#148397) next »