Alternative to this code?

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

    Alternative to this code?

    I know there is a better way to do something like this in PHP:

    if (($color == "red") || ($color == "blue") || ($color == "white") ||
    ($color == "yellow")) { .. }

    Is there a way I can condense that so I don't have to write $color
    everytime?
  • 

    #2
    Re: Alternative to this code?

    $colors = array(
    'red',
    'blue',
    'white',
    'yellow
    );
    if (in_array($colo r, $colors))
    {
    //...
    }


    "Dave Thomas" <gentso@hotmail .com> wrote in message
    news:1dqdnQiykp 9dSJ3eRVn-iw@adelphia.com ...
    |I know there is a better way to do something like this in PHP:
    |
    | if (($color == "red") || ($color == "blue") || ($color == "white") ||
    | ($color == "yellow")) { .. }
    |
    | Is there a way I can condense that so I don't have to write $color
    | everytime?


    Comment

    • Colin McKinnon

      #3
      Re: Alternative to this code?

      Dave Thomas wrote:
      [color=blue]
      > I know there is a better way to do something like this in PHP:
      >
      > if (($color == "red") || ($color == "blue") || ($color == "white") ||
      > ($color == "yellow")) { .. }
      >
      > Is there a way I can condense that so I don't have to write $color
      > everytime?[/color]
      switch ($color) {
      case 'red':
      case 'blue':
      case 'yellow':
      case 'white':
      print "yes, its one on those\n";
      break;
      case 'black':
      default:
      print "No, its something else\n";
      break;
      }

      HTH

      C.

      Comment

      • kholm@dis.umsmed.edu

        #4
        Re: Alternative to this code?

        Yet another method:

        $pattern = '/^(red|blue|whit e|yellow)$/';

        if (preg_match($pa ttern, $color)) {
        ...
        }

        Comment

        • drwxr-xr-x

          #5
          Re: Alternative to this code?

          On 15 Aug 2005 13:40:06 -0700, kholm@dis.umsme d.edu wrote:[color=blue]
          > Yet another method:
          >
          > $pattern = '/^(red|blue|whit e|yellow)$/';
          >
          > if (preg_match($pa ttern, $color)) {
          > ...
          > }[/color]

          Just _what_ are you nattering on about?
          Oh, I see the problem:
          X-Trace: posting.google. com

          Straighten up and fly right:


          Comment

          • Cristian Gutierrez

            #6
            Re: Alternative to this code?

            Hoy en la tarde, Dave Thomas dijo:[color=blue]
            > I know there is a better way to do something like this in PHP:
            >
            > if (($color == "red") || ($color == "blue") || ($color == "white") ||
            > ($color == "yellow")) { .. }
            >
            > Is there a way I can condense that so I don't have to write $color
            > everytime?[/color]

            ,----
            | // can also be loaded from somewhere else, say, a db or file
            | $available_colo rs = array('red', 'blue', 'white', 'yellow');
            |
            | if (in_array($colo r, $available_colo rs)) {
            | ...
            | }
            `----

            --
            Cristian Gutierrez /* crgutier@dcc.uc hile.cl */
            "Vampirewar e: /n/, a project, capable of sucking the lifeblood out of
            anyone unfortunate enough to be assigned to it, which never actually
            sees the light of day, but nonetheless refuses to die."

            Comment

            • Ken Robinson

              #7
              Re: Alternative to this code?

              Dave Thomas <gentso@hotmail .com> wrote in news:1dqdnQiykp 9dSJ3eRVn-
              iw@adelphia.com:
              [color=blue]
              > I know there is a better way to do something like this in PHP:
              >
              > if (($color == "red") || ($color == "blue") || ($color == "white") ||
              > ($color == "yellow")) { .. }
              >
              > Is there a way I can condense that so I don't have to write $color
              > everytime?
              >[/color]

              You could use a switch statement:
              <?php
              switch ($color) {
              case 'red':
              case 'blue':
              case 'white':
              case 'yellow':
              //
              // do your stuff here
              //
              break;
              case 'somethingelse' :
              echo 'what color is this???';
              break;
              }
              ?>

              Ken

              Comment

              • 

                #8
                Re: Alternative to this code?

                | Just _what_ are you nattering on about?
                | Oh, I see the problem:

                so you just jump into this conversation for the sole purpose OF nattering
                about HOW someone else posts?!!! don't be a tard! get a good news reader and
                you won't have to worry about following the thread. even BETTER, mind your
                OWN behavior AND actually post content that addresses the op's concern.
                that's more offensive than having someone not summarizing/referencing a
                previous post to which he comments.

                foad.


                Comment

                Working...