RE: [PHP] wierd if statement problem
| From: | electroteque | Date: | Wed, 21 May 2003 11:42:01 +0000 |
| Subject: | RE: [PHP] wierd if statement problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-148399@lists.php.net to get a copy of this message | ||
yeh i eventually worked out this worked
if (($_SESSION['groupID']!==1) && ($_SESSION['groupID']!==2))
instead of || , it was getting late in the day , sorry guys for the chaos of
a simple boolean statement thanks heaps everyone
-----Original Message-----
From: Ford, Mike [LSS] [mailto:M.Ford@lmu.ac.uk]
Sent: Wednesday, May 21, 2003 8:29 PM
To: 'daniel@electroteque.org'
Cc: php-general@lists.php.net; php-general@gremlins.biz
Subject: RE: [PHP] wierd if statement problem
> -----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