Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP Switch issue

  • 18-07-2009 11:13AM
    #1
    Registered Users, Registered Users 2 Posts: 4,475 ✭✭✭


    I have some code where I'm testing the value of something
    switch ($p) {
     case 0:
      do something
      break;
    
     case 1:
      do something else
      break;
    
     case 2:
      do a third thing
      break;
    
     default:
      do a fourth thing
      break;
    }
    
    Now my issue is that if $p is 1 or 2, I also want to do what's in the default case. Normally that works fine if I remove the break; from the case, but not when there's 2 cases, it's just running from case 1 to case 2 to default, obviously. It seems a minor thing but I'm not able to find a fix for it anywhere.


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    I imagine your only two options are to stick an if after the switch statement, or include the same line(s) in the 1st and 2nd cases as the default case.


  • Registered Users, Registered Users 2 Posts: 4,475 ✭✭✭corblimey


    seamus wrote: »
    I imagine your only two options are to stick an if after the switch statement, or include the same line(s) in the 1st and 2nd cases as the default case.

    Dang


  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    Because of the way PHP goes through switch() statements sequentially, you could just leave cases 1 and 2 at the bottom, just above default, and omit the break; from case 1 and case 2 and put an if() in case 2 to ensure it's not case 1 falling through.
    switch ($p) {
     case 0:
      do something
      break;
    
     case 1:
      do something else
     case 2:
      if(case 2) {
        do a third thing
      }
     default:
      do a fourth thing
      break;
    }
    

    In most other languages, you'd just define a label in default and goto() it, but PHP won't let you goto() into a loop or switch statement. Although, technically, you're already in the switch() scope, so maybe it'd work. Try it.
    switch ($p) {
     case 0:
      do something
      break;
    
     case 1:
      do something else
      goto cheapassgotolabel;
      break;
    
     case 2:
      do a third thing
      goto cheapassgotolabel;
      break;
    
     default:
      cheapassgotolabel:
      do a fourth thing
      break;
    }
    

    Yes, I said goto(). Sometimes it's okay :D

    0baa1b9fae6aec55bbb73037f3016001-xkcd-goto.png


  • Registered Users, Registered Users 2 Posts: 4,475 ✭✭✭corblimey


    Sparks wrote: »
    0baa1b9fae6aec55bbb73037f3016001-xkcd-goto.png

    Bleedin' velociraptors all over the shop now, cheers Sparks :P


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    As a matter of fact, the goto statement has being introduced in PHP 5.3. Can be useful at times but better off avoid if possible.
    They've included that comic too :pac:

    http://ie.php.net/goto


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    You mean you're not using 5.3? :D


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Sorry I miss read your post. Thought you said there was no goto, but yeah you can't go into a switch or loop structure. However, have you tried doing a jump within a switch? - Never know, you can jump out too


  • Registered Users, Registered Users 2 Posts: 4,475 ✭✭✭corblimey


    Thanks WebMonkey, Sparks, for various reasons, I gave up and resorted to sodding IF statements. It just got too complicated - there were cases where I wanted to do A then B then D, but sometimes C, etc. Too much. I have better control with prioritised IF's.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Yes, switches are generally only used when it's one and only case or there's a sequential flow where you can place breaks in neccessary positions. If's prob provide more freedom. Best luck


Advertisement