checking multiple conditions for one variable

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

    #1

    checking multiple conditions for one variable

    What I need to do is a comparison to see if a variable equals none of
    the 48 contiguous United States, but as efficiently as possible.

    The best way I can come up with the the textbook method of :

    if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
    ($state != "CO") ...etc ) {

    Is there some way to shorten that a bit to do:

    if ($state != "AR" && "AK" && "AL" && ...etc ) {

    I looked at php.net for logical operators but couldn't find anything
    that seemed to apply.

    If someone can just tell me the keyword to look up in php.net I'd
    appreciate it.
    Like "search 'foobar', newbie" would be fine. =)

    Thanks!!
    Liam
  • Urs Weder

    #2
    Re: checking multiple conditions for one variable

    LRW wrote:[color=blue]
    > What I need to do is a comparison to see if a variable equals none of
    > the 48 contiguous United States, but as efficiently as possible.
    >
    > The best way I can come up with the the textbook method of :
    >
    > if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
    > ($state != "CO") ...etc ) {
    >
    > Is there some way to shorten that a bit to do:
    >
    > if ($state != "AR" && "AK" && "AL" && ...etc ) {
    >
    > I looked at php.net for logical operators but couldn't find anything
    > that seemed to apply.
    >
    > If someone can just tell me the keyword to look up in php.net I'd
    > appreciate it.
    > Like "search 'foobar', newbie" would be fine. =)
    >
    > Thanks!!
    > Liam[/color]

    "array_sear ch" is your keyword:

    array_search() example
    <?php
    $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

    $key = array_search('g reen', $array); // $key = 2;
    $key = array_search('r ed', $array); // $key = 1;
    ?>

    Greetings, Urs

    --
    +-------------------------
    | Urs Weder
    | N 47°23'23" E 9°39'47"
    +-------------------------
    ( modify address for return email )

    Comment

    • Malcolm Dew-Jones

      #3
      Re: checking multiple conditions for one variable

      LRW (deja@celticbea r.com) wrote:
      : What I need to do is a comparison to see if a variable equals none of
      : the 48 contiguous United States, but as efficiently as possible.

      : The best way I can come up with the the textbook method of :

      : if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
      : ($state != "CO") ...etc ) {

      : Is there some way to shorten that a bit to do:

      : if ($state != "AR" && "AK" && "AL" && ...etc ) {

      : I looked at php.net for logical operators but couldn't find anything
      : that seemed to apply.

      : If someone can just tell me the keyword to look up in php.net I'd
      : appreciate it.
      : Like "search 'foobar', newbie" would be fine. =)

      something like

      $valid_states["AR"]=1;
      $valid_states["AK"]=1;
      $valid_states["AL"]=1;
      $valid_states["CO"]=1;
      # ...etc...

      $state = "AB";
      if ( $valid_states[ $state ] == 1 )
      {
      echo "$state is valid\n";
      }

      $state = "CO";
      if ( $valid_states[ $state ] == 1 )
      {
      echo "$state is valid\n";
      }


      Comment

      • CJ Llewellyn

        #4
        Re: checking multiple conditions for one variable


        "LRW" <deja@celticbea r.com> wrote in message
        news:3a1d1813.0 406221352.6faec 47f@posting.goo gle.com...[color=blue]
        > What I need to do is a comparison to see if a variable equals none of
        > the 48 contiguous United States, but as efficiently as possible.
        >
        > The best way I can come up with the the textbook method of :
        >
        > if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
        > ($state != "CO") ...etc ) {
        >
        > Is there some way to shorten that a bit to do:
        >
        > if ($state != "AR" && "AK" && "AL" && ...etc ) {
        >
        > I looked at php.net for logical operators but couldn't find anything
        > that seemed to apply.
        >
        > If someone can just tell me the keyword to look up in php.net I'd
        > appreciate it.
        > Like "search 'foobar', newbie" would be fine. =)[/color]

        define (STATES "AR,AK,AL,NY,") ;

        $state = ucase($state);
        if(! strpos(STATES , $state))
        {

        }

        you can use stripos in php5


        Comment

        • Guest's Avatar

          #5
          Re: checking multiple conditions for one variable

          > The best way I can come up with the the textbook method of :[color=blue]
          >
          > if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
          > ($state != "CO") ...etc ) {
          >
          > Is there some way to shorten that a bit to do:
          >
          > if ($state != "AR" && "AK" && "AL" && ...etc ) {[/color]

          Try the following:

          <?php

          $state = 'CA';
          $states = array('AR','AK' ,'AL','CO');

          if ( in_array($state , $states) ) {
          printf('I have found : %s in : %s', $state, join(',', $states));
          } else {
          printf('I did not find : %s in : %s', $state, join(',', $states));
          }

          ?>

          --Wil


          Comment

          • Chung Leong

            #6
            Re: checking multiple conditions for one variable

            "Malcolm Dew-Jones" <yf110@vtn1.vic toria.tc.ca> wrote in message
            news:40d8b4ac@n ews.victoria.tc .ca...[color=blue]
            > LRW (deja@celticbea r.com) wrote:
            > : What I need to do is a comparison to see if a variable equals none of
            > : the 48 contiguous United States, but as efficiently as possible.
            >
            > : The best way I can come up with the the textbook method of :
            >
            > : if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
            > : ($state != "CO") ...etc ) {
            >
            > : Is there some way to shorten that a bit to do:
            >
            > : if ($state != "AR" && "AK" && "AL" && ...etc ) {
            >
            > : I looked at php.net for logical operators but couldn't find anything
            > : that seemed to apply.
            >
            > : If someone can just tell me the keyword to look up in php.net I'd
            > : appreciate it.
            > : Like "search 'foobar', newbie" would be fine. =)
            >
            > something like
            >
            > $valid_states["AR"]=1;
            > $valid_states["AK"]=1;
            > $valid_states["AL"]=1;
            > $valid_states["CO"]=1;
            > # ...etc...
            >
            > $state = "AB";
            > if ( $valid_states[ $state ] == 1 )
            > {
            > echo "$state is valid\n";
            > }
            >
            > $state = "CO";
            > if ( $valid_states[ $state ] == 1 )
            > {
            > echo "$state is valid\n";
            > }[/color]

            Yup, I was going to suggest that. A hash lookup is a lot quicker than
            array_search() or in_array(). To simplify the code I would do this:

            $states = array("AR", "AK", "AL", "CO" ... );
            $states_inv = array_flip($sta tes);
            if(isset($state s_inv[$state])) {
            echo "$state is valid\n";
            }

            But since there are only 50 items though, perhaps in_array() isn't that bad.


            Comment

            • LRW

              #7
              Re: checking multiple conditions for one variable

              "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<YfednZKZi qJ1ZkXdRVn_iw@c omcast.com>...[color=blue]
              > "Malcolm Dew-Jones" <yf110@vtn1.vic toria.tc.ca> wrote in message
              > news:40d8b4ac@n ews.victoria.tc .ca...[color=green]
              > > something like
              > >
              > > $valid_states["AR"]=1;
              > > $valid_states["AK"]=1;
              > > $valid_states["AL"]=1;
              > > $valid_states["CO"]=1;
              > > # ...etc...
              > >
              > > $state = "AB";
              > > if ( $valid_states[ $state ] == 1 )
              > > {
              > > echo "$state is valid\n";
              > > }[/color]
              >
              > Yup, I was going to suggest that. A hash lookup is a lot quicker than
              > array_search() or in_array(). To simplify the code I would do this:
              >
              > $states = array("AR", "AK", "AL", "CO" ... );
              > $states_inv = array_flip($sta tes);
              > if(isset($state s_inv[$state])) {
              > echo "$state is valid\n";
              > }
              >
              > But since there are only 50 items though, perhaps in_array() isn't that bad.[/color]

              Thanks, some great info!
              Much appreciated!

              Liam

              Comment

              Working...