Switch question

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

    Switch question

    In some languages, you can do the following in a Switch statement...is there
    a way to do this in PHP? Thanks in advance

    switch($voterac tion){
    case '1','2','3','4' ,'5','6','7','8 ','9','10':
    $db_set .= "Rating = $voteraction, ";
    break;
    }

    or do I have to the following:
    switch($voterac tion){
    case '1':
    $db_set .= "Rating = $voteraction, ";
    break;
    case '2':
    $db_set .= "Rating = $voteraction, ";
    break;
    case '3':
    $db_set .= "Rating = $voteraction, ";
    break;
    etc....
    }

    --

    *************** ******
    Jon Rosenberg
    Kênh trực tiếp bóng đá Cakhiaz.co✅ Link xem bóng đá trực tuyến hôm nay cùng đội ngũ bình luận viên nhiệt huyết cho fan xem tructiepbongda tốc độ cao.




  • Andy Hassall

    #2
    Re: Switch question

    On Sat, 15 Nov 2003 19:16:05 GMT, "Jon" <ruffles_@msn.c om> wrote:
    [color=blue]
    >In some languages, you can do the following in a Switch statement...is there
    >a way to do this in PHP? Thanks in advance
    >
    >switch($votera ction){
    >case '1','2','3','4' ,'5','6','7','8 ','9','10':
    > $db_set .= "Rating = $voteraction, ";
    > break;
    >}
    >
    >or do I have to the following:
    >switch($votera ction){
    >case '1':
    > $db_set .= "Rating = $voteraction, ";
    > break;
    >case '2':
    > $db_set .= "Rating = $voteraction, ";
    > break;
    >case '3':
    > $db_set .= "Rating = $voteraction, ";
    > break;
    >etc....
    >}[/color]

    switch($voterac tion){
    case '1':
    case '2':
    case '3':
    $db_set .= "Rating = $voteraction, ";
    break;
    }



    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • Terence

      #3
      Re: Switch question

      Jon wrote:
      [color=blue]
      > In some languages, you can do the following in a Switch statement...is there
      > a way to do this in PHP? Thanks in advance
      >
      > switch($voterac tion){
      > case '1','2','3','4' ,'5','6','7','8 ','9','10':
      > $db_set .= "Rating = $voteraction, ";
      > break;
      > }
      >[/color]

      an alternative could be

      if(in_array($vo teraction,array ('1','2','3','4 ','5','6','7',' 8','9','10'))) {
      // whatever
      }
      elseif(...)

      another alternative could be a regular expression match but my knowledge
      is very shakey in that area. Perhaps something like the following
      (someone with regex knowledge would have to confirm this).

      if (preg_match("/^\d.$/", $voteraction)) {
      // whatever
      }

      Comment

      • Disco Plumber

        #4
        Re: Switch question

        Andy Hassall (73.075% quality rating):[color=blue]
        >
        > switch($voterac tion){
        > case '1':
        > case '2':
        > case '3':
        > $db_set .= "Rating = $voteraction, ";
        > break;
        > }[/color]

        If you have to do a huge number of cases, a somewhat less classy-looking
        way would be (assuming voteraction is an int):

        switch(true) {
        case (1<=$voteractio n && $voteraction<=1 0):
        $db_set .= "Rating = $voteraction, ";
        break;
        ...

        If it's not an int, you could cast it to one first, use floor(), or whatever.

        Of course, if you're doing the above, you're probably better off with
        if-else blocks.

        /joe
        --
        Andy James practically disrespects the phatcave. In Krispy Kreme, the
        triple-poorly-executed emo kid is uberordinary.

        Comment

        • Chris Haynes

          #5
          Re: Switch question

          "Jon" <ruffles_@msn.c om> wrote in message news:<VXutb.136 1$_i1.849053@ne ws2.news.adelph ia.net>...
          [color=blue]
          > In some languages, you can do the following in a Switch statement...is there
          > a way to do this in PHP? Thanks in advance
          >
          > switch($voterac tion){
          > case '1','2','3','4' ,'5','6','7','8 ','9','10':
          > $db_set .= "Rating = $voteraction, ";
          > break;
          > }[/color]

          I'd use:

          switch($voterac tion){
          case 1: case 2: case 3: case...:
          $db_set .= "Rating = $voteraction, ";
          break;
          }

          Cheers,

          Chris.

          Comment

          Working...