Multiple (OR ||) case usage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Uleric

    Multiple (OR ||) case usage

    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.

  • Rik

    #2
    Re: Multiple (OR ||) case usage

    Uleric <Uleric@gmail.c omwrote:
    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.
    Unfortunately, this is not how conditionals work.
    You first statement breaks down like:
    $myzone !== ("here") -can be true or false
    or
    ("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 !== "here" ||
    $myzone == "there" ||
    $myzone == "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 == '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

    Comment

    • Curtis

      #3
      Re: Multiple (OR ||) case usage

      On Wed, 31 Jan 2007 17:35:25 -0800, Rik <luiheidsgoeroe @hotmail.comwro te:
      Uleric <Uleric@gmail.c omwrote:
      >
      >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.
      >
      Unfortunately, this is not how conditionals work.
      You first statement breaks down like:
      $myzone !== ("here") -can be true or false
      or
      ("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 !== "here" ||
      $myzone == "there" ||
      $myzone == "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 == '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.
      There is an error in both snippets of code "die:" is used, you need end
      the statement with a semi-colon ";"

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

      Concerning the logic, $myzone !== 'here' is not even needed, becauseif
      $myzone equals 'there' or 'where' it can't be 'here'. I agree, though, it
      seems like the OP is actually doing something more complex. Also, only the
      first boolean comparison checks against type, which seems unnecessary here.

      --
      Curtis, http://dyersweb.com

      Comment

      • Toby A Inkster

        #4
        Re: Multiple (OR ||) case usage

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

        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
        slower!):

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


        --
        Toby A Inkster BSc (Hons) ARCS
        Contact Me ~ http://tobyinkster.co.uk/contact
        Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

        * = I'm getting there!

        Comment

        Working...