Multiple (OR ||) case usage

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Having trouble wrapping this up into a viable statement

I want to check on a users status before allowing a script to be run.
$myzone can only be 1 variable, but it can be here, there or where.

if ($myzone !== ("here") or ("there") or ("where")) { die: }

This does not work however, it will kill the script regardless if one
of the statments is actually true.

However, this WILL work if only one operator is checked:

if ($myzone !== "here") { die: }

If $myzone is "here" the script is allowed to run. If it's anything
else, it dies, as it is supposed to. However, I want to check if
$myzone is several options. That is my delima.

Re: Multiple (OR ||) case usage

Quoted text here. Click to load it

Unfortunately, this is not how conditionals work.
You first statement breaks down like:
$myzone !=3D=3D ("here")  -> can be true or false
("there") -> a string, which cast to a boolean will always be true, no  =

further evaluation will be done, conditions are met, code die()s .

You could use a lengthy version:
if (
    $myzone !=3D=3D "here" ||
    $myzone =3D "there" ||
    $myzone =3D "where"){ die: }

Then again, I don't understand you logic. The script is only allowed to =

run when it's 'here', so why exactly fo you want to check the rest? If  =

$myzone =3D 'here', it cannot be anything else, unless your trying  =

something more complex then the example offcourse, in which case I'd be =

interested in what exactly.
-- =

Rik Wasmus

Re: Multiple (OR ||) case usage


Quoted text here. Click to load it


Quoted text here. Click to load it
o  =

Quoted text here. Click to load it

Quoted text here. Click to load it

Quoted text here. Click to load it
e  =

Quoted text here. Click to load it

There is an error in both snippets of code "die:" is used, you need end =

the statement with a semi-colon ";"

   if ($myzone =3D 'there' || $myzone =3D 'where') die;

Concerning the logic, $myzone !=3D=3D 'here' is not even needed, because=
 if  =

$myzone equals 'there' or 'where' it can't be 'here'. I agree, though, i=
t  =

seems like the OP is actually doing something more complex. Also, only t=
he  =

first boolean comparison checks against type, which seems unnecessary he=

-- =


Re: Multiple (OR ||) case usage

Uleric wrote:

Quoted text here. Click to load it


    if ($myzone!='here' && $myzone!='there' && $myzone!='where') die;

Or, using De Morgan's Laws (boolean algebra law concerning the
relationship between NOT, AND and OR) you could alternatively use:

    if (!( $myzone=='here' || $myzone=='there' || $myzone=='where' )) die;

Or, to make your code read a bit more sanely (but might actually run

    $allowed_states = array('here', 'there', 'where');
    if (!in_array($myzone, $allowed_states)) die;

Toby A Inkster BSc (Hons) ARCS
Contact Me ~
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

Site Timeline