Problem with 'Switch'

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

    Problem with 'Switch'

    I have using a 'switch' to retrieve the name of the website section through
    sending a 'section' integer...

    function title($section)
    {
    switch ($section)
    {
    case 0:
    $output = "Introducti on";
    case 1:
    $output = "login";
    case 2:
    $output = "choose_dat e";
    case 3:
    $output = "am_pm";
    case 4:
    $output = "hour";
    case 5:
    $output = "choose_tim e";
    case 6:
    $output = "slots";
    case 7:
    $output = "add_notes" ;
    case 8:
    $output = "confirm";
    case 9:
    $output = "booked";
    default:
    $output = "Introducti on";
    }
    return $output;
    }

    However, no matter what number I sent to the command I am getting the
    default case out (Introduction).

    This seems like a very simple command, where am I going wrong???

    Many thanks

    Jamie


  • Pedro Graca

    #2
    Re: Problem with 'Switch'

    Jamie Wright wrote:[color=blue]
    > I have using a 'switch' to retrieve the name of the website section through
    > sending a 'section' integer...
    >
    > function title($section)
    > {
    > switch ($section)
    > {
    > case 0:
    > $output = "Introducti on";
    > case 1:
    > $output = "login";[/color]
    (snip)[color=blue]
    > default:
    > $output = "Introducti on";
    > }
    > return $output;
    > }
    >
    > However, no matter what number I sent to the command I am getting the
    > default case out (Introduction).
    >
    > This seems like a very simple command, where am I going wrong???[/color]

    switch's cases fall through to next case, so that you can do

    switch($a) {
    case 0:
    case 1:
    whatever();
    break;
    case 2:
    case 3:
    somethingelse() ;
    break;
    default:
    nomatch();
    break;
    }

    Here, whatever() will be called when $a is 0 or 1; somethingelse() will
    be called when $a is 2 or 3; and nomatch() will be called at all other
    times.

    In your script, when $section is 0, it will set $output to
    "Introducti on", then to "login", then ..., and finally to "Introducti on"
    again.
    You need break statements in each case to stop the script from going on
    to next case statements.


    --
    USENET would be a better place if everybody read: | to email me: use |
    http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
    http://www.netmeister.org/news/learn2quote2.html | header, textonly |
    http://www.expita.com/nomime.html | no attachments. |

    Comment

    • Jamie Wright

      #3
      Re: Problem with 'Switch'


      "Pedro Graca" <hexkid@hotpop. com> wrote in message
      news:slrnceg056 .16c.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
      > Jamie Wright wrote:[color=green]
      > > I have using a 'switch' to retrieve the name of the website section[/color][/color]
      through[color=blue][color=green]
      > > sending a 'section' integer...
      > >
      > > function title($section)
      > > {
      > > switch ($section)
      > > {
      > > case 0:
      > > $output = "Introducti on";
      > > case 1:
      > > $output = "login";[/color]
      > (snip)[color=green]
      > > default:
      > > $output = "Introducti on";
      > > }
      > > return $output;
      > > }
      > >
      > > However, no matter what number I sent to the command I am getting the
      > > default case out (Introduction).
      > >
      > > This seems like a very simple command, where am I going wrong???[/color]
      >
      > switch's cases fall through to next case, so that you can do
      >
      > switch($a) {
      > case 0:
      > case 1:
      > whatever();
      > break;
      > case 2:
      > case 3:
      > somethingelse() ;
      > break;
      > default:
      > nomatch();
      > break;
      > }
      >
      > Here, whatever() will be called when $a is 0 or 1; somethingelse() will
      > be called when $a is 2 or 3; and nomatch() will be called at all other
      > times.
      >
      > In your script, when $section is 0, it will set $output to
      > "Introducti on", then to "login", then ..., and finally to "Introducti on"
      > again.
      > You need break statements in each case to stop the script from going on
      > to next case statements.
      >
      >
      > --
      > USENET would be a better place if everybody read: | to email me: use |
      > http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
      > http://www.netmeister.org/news/learn2quote2.html | header, textonly |
      > http://www.expita.com/nomime.html | no attachments. |[/color]

      Brilliant. Thanks

      Jamie


      Comment

      • Chung Leong

        #4
        Re: Problem with 'Switch'

        To paraphrase Detective Deckard, how can a page not know what it is?

        "Jamie Wright" <anon@anon.co m> wrote in message
        news:cc8tnu$arn $1@news8.svr.po l.co.uk...[color=blue]
        > I have using a 'switch' to retrieve the name of the website section[/color]
        through[color=blue]
        > sending a 'section' integer...
        >
        > function title($section)
        > {
        > switch ($section)
        > {
        > case 0:
        > $output = "Introducti on";
        > case 1:
        > $output = "login";
        > case 2:
        > $output = "choose_dat e";
        > case 3:
        > $output = "am_pm";
        > case 4:
        > $output = "hour";
        > case 5:
        > $output = "choose_tim e";
        > case 6:
        > $output = "slots";
        > case 7:
        > $output = "add_notes" ;
        > case 8:
        > $output = "confirm";
        > case 9:
        > $output = "booked";
        > default:
        > $output = "Introducti on";
        > }
        > return $output;
        > }
        >
        > However, no matter what number I sent to the command I am getting the
        > default case out (Introduction).
        >
        > This seems like a very simple command, where am I going wrong???
        >
        > Many thanks
        >
        > Jamie
        >
        >[/color]


        Comment

        • Pjotr Wedersteers

          #5
          Re: Problem with 'Switch'


          "Jamie Wright" <anon@anon.co m> wrote in message
          news:cc90h8$cv9 $1@news8.svr.po l.co.uk...[color=blue]
          >
          > "Pedro Graca" <hexkid@hotpop. com> wrote in message
          > news:slrnceg056 .16c.hexkid@ID-203069.user.uni-berlin.de...[color=green]
          > > Jamie Wright wrote:[color=darkred]
          > > > I have using a 'switch' to retrieve the name of the website section[/color][/color]
          > through[color=green][color=darkred]
          > > > sending a 'section' integer...
          > > >
          > > > function title($section)
          > > > {
          > > > switch ($section)
          > > > {
          > > > case 0:
          > > > $output = "Introducti on";
          > > > case 1:
          > > > $output = "login";[/color]
          > > (snip)[color=darkred]
          > > > default:
          > > > $output = "Introducti on";
          > > > }
          > > > return $output;
          > > > }
          > > >
          > > > However, no matter what number I sent to the command I am getting the
          > > > default case out (Introduction).
          > > >
          > > > This seems like a very simple command, where am I going wrong???[/color]
          > >
          > > switch's cases fall through to next case, so that you can do
          > >
          > > switch($a) {
          > > case 0:
          > > case 1:
          > > whatever();
          > > break;
          > > case 2:
          > > case 3:
          > > somethingelse() ;
          > > break;
          > > default:
          > > nomatch();
          > > break;
          > > }
          > >
          > > Here, whatever() will be called when $a is 0 or 1; somethingelse() will
          > > be called when $a is 2 or 3; and nomatch() will be called at all other
          > > times.
          > >
          > > In your script, when $section is 0, it will set $output to
          > > "Introducti on", then to "login", then ..., and finally to "Introducti on"
          > > again.
          > > You need break statements in each case to stop the script from going on
          > > to next case statements.
          > >
          > >
          > > --
          > > USENET would be a better place if everybody read: | to email me: use |
          > > http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
          > > http://www.netmeister.org/news/learn2quote2.html | header, textonly |
          > > http://www.expita.com/nomime.html | no attachments. |[/color]
          >
          > Brilliant. Thanks
          >
          > Jamie
          >[/color]
          You already got the switch/break answer, but if all you want to do is assign
          a string constant, you could also have used an array filled with the 10
          strings you need and use the $a as the index.

          $arr = array("Introduc tion","login"," choose_date","a m_pm","hour"); // add
          the other 5 as well of course...
          $output = $arr [$a];

          Less typing, and I think more elegant as well. I haven't tried which
          solution is faster at runtime. My guess would be the array version too.

          HTH
          Pjotr
          HTH
          Pjotr


          Comment

          • CJ Llewellyn

            #6
            Re: Problem with 'Switch'

            "Pjotr Wedersteers" <x33159@westert erp.com> wrote in message
            news:40e90914$0 $24462$e4fe514c @dreader10.news .xs4all.nl...
            -snip-[color=blue]
            > $arr = array("Introduc tion","login"," choose_date","a m_pm","hour"); // add
            > the other 5 as well of course...
            > $output = $arr [$a];
            >
            > Less typing, and I think more elegant as well. I haven't tried which
            > solution is faster at runtime.[/color]

            And vulnerable to attack

            $a = 9999;
            echo $arr[$a];



            Comment

            Working...