If Or

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

    If Or

    I need to test for one of several values. Can I include these in a
    single "if" statement

    I tried using OR but it does not work. In the actual file I need to
    check if a supplied user input meets one of about 170 possible code
    combinations.

    This does not work

    if ($var == 1 OR 3 OR 7 OR 8 OR 9 OR "Table" OR "Chair"){
    echo 'yes';
    } else {
    echo 'no';
    }

    A long winded way to do it and the only way I know how to use OR is...

    if ($var == 1 || $var == 3 || $var == 7 || $var == 7 || $var == 9 ||
    $var == "Table" || $var == "Chair")
    ....etc

    But I would like to just list the values (and not the $var == for
    every possible condition

    Garry Jones
    Sweden

  • Geoff Berrow

    #2
    Re: If Or

    Message-ID: <1176901125.935 895.207100@b58g 2000hsg.googleg roups.comfrom
    GarryJones contained the following:
    >A long winded way to do it and the only way I know how to use OR is...
    >
    >if ($var == 1 || $var == 3 || $var == 7 || $var == 7 || $var == 9 ||
    >$var == "Table" || $var == "Chair")
    >...etc
    >
    >But I would like to just list the values (and not the $var == for
    >every possible condition
    Put the values in an array and use in_array()

    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • Tyno Gendo

      #3
      Re: If Or

      GarryJones wrote:
      I need to test for one of several values. Can I include these in a
      single "if" statement
      >
      I tried using OR but it does not work. In the actual file I need to
      check if a supplied user input meets one of about 170 possible code
      combinations.
      >
      This does not work
      >
      if ($var == 1 OR 3 OR 7 OR 8 OR 9 OR "Table" OR "Chair"){
      echo 'yes';
      } else {
      echo 'no';
      }
      >
      A long winded way to do it and the only way I know how to use OR is...
      >
      if ($var == 1 || $var == 3 || $var == 7 || $var == 7 || $var == 9 ||
      $var == "Table" || $var == "Chair")
      ...etc
      >
      But I would like to just list the values (and not the $var == for
      every possible condition
      >
      Garry Jones
      Sweden
      >
      if ( in_array( $var, array( 1, 3, 7, 9) ) {
      // do something
      }

      Comment

      • Steve

        #4
        Re: If Or

        well, this is the best i can offer based on what you've described...

        function checkInput($val ue)
        {
        static $combination = array(
        1 ,
        3 ,
        7 ,
        8 ,
        9 ,
        'Table' ,
        'Chair'
        )
        return in_array($value , $combination, true);
        }

        echo checkInput($var ) ? 'yes' : 'no';


        that's one way. and, you could build $combination from a db so that the
        function scales. another way would be to write (or store and retrieve) a
        regular expression or series of them:

        /^[13789]|table|chair$/gi

        then you'd preg_match a true or false answer from that.

        hth,

        me


        "GarryJones " <morack@algonet .sewrote in message
        news:1176901125 .935895.207100@ b58g2000hsg.goo glegroups.com.. .
        |I need to test for one of several values. Can I include these in a
        | single "if" statement
        |
        | I tried using OR but it does not work. In the actual file I need to
        | check if a supplied user input meets one of about 170 possible code
        | combinations.
        |
        | This does not work
        |
        | if ($var == 1 OR 3 OR 7 OR 8 OR 9 OR "Table" OR "Chair"){
        | echo 'yes';
        | } else {
        | echo 'no';
        | }
        |
        | A long winded way to do it and the only way I know how to use OR is...
        |
        | if ($var == 1 || $var == 3 || $var == 7 || $var == 7 || $var == 9 ||
        | $var == "Table" || $var == "Chair")
        | ...etc
        |
        | But I would like to just list the values (and not the $var == for
        | every possible condition
        |
        | Garry Jones
        | Sweden
        |


        Comment

        • Mitul

          #5
          Re: If Or

          On Apr 18, 5:58 pm, GarryJones <mor...@algonet .sewrote:
          I need to test for one of several values. Can I include these in a
          single "if" statement
          >
          I tried using OR but it does not work. In the actual file I need to
          check if a supplied user input meets one of about 170 possible code
          combinations.
          >
          This does not work
          >
          if ($var == 1 OR 3 OR 7 OR 8 OR 9 OR "Table" OR "Chair"){
          echo 'yes';
          >
          } else {
          echo 'no';
          }
          >
          A long winded way to do it and the only way I know how to use OR is...
          >
          if ($var == 1 || $var == 3 || $var == 7 || $var == 7 || $var == 9 ||
          $var == "Table" || $var == "Chair")
          ...etc
          >
          But I would like to just list the values (and not the $var == for
          every possible condition
          >
          Garry Jones
          Sweden
          Hello,

          If you have array of all possible 170 values then you can try with
          in_array function. If you don't have array then prepare it and then
          check it.

          Comment

          • gosha bine

            #6
            Re: If Or

            On 18.04.2007 14:58 GarryJones wrote:
            I need to test for one of several values. Can I include these in a
            single "if" statement
            >
            I tried using OR but it does not work. In the actual file I need to
            check if a supplied user input meets one of about 170 possible code
            combinations.
            Hardcoding more than 100 conditions doesn't seem to be a good idea in
            any case. Put them in db and use the power of sql to find if the value
            matches.


            --
            gosha bine

            extended php parser ~ http://code.google.com/p/pihipi
            blok ~ http://www.tagarga.com/blok

            Comment

            Working...