SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    The short answer is yes... Herbster's Avatar
    Join Date
    Oct 2001
    Location
    Bay City, Oregon
    Posts
    715
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Nesting Switch Control Structures?

    Is it possible to nest switch/case control structures?
    If so, could someone point me at some example code?
    Can't find a reference in the docs. Getting weird parse errors. Expected T_CASE, T_BREAK or T_ENDSWITCH.
    Now I'm getting this one:
    Parse error: parse error, expecting `ENDSWITCH'
    when PHP encounters the first case statement in the nested switch - or is it really complaining about the switch statement in the previous line because nesting is not allowed?

    I'll post code if it comes to that, but for right now I'd just like to educate myself a bit.

    tia

  2. #2
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well if you want to educate yourself here is the official PHP reference for switch case's.
    http://www.php.net/manual/en/control...res.switch.php
    -Jordan

  3. #3
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    // index.php
    switch($mode) {
      case 
    "test":
      
      switch(
    $f) {
        case 
    "one":
        echo(
    'Case One');
        break;

        default:

        echo(
    'Welcome!');
      }
      
      break;
    }
    ?>
    Then go to url.php?mode=test&f=one
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  4. #4
    The short answer is yes... Herbster's Avatar
    Join Date
    Oct 2001
    Location
    Bay City, Oregon
    Posts
    715
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks JordanTLClive -
    Actually that is where I did start.
    Just couldn't find greater detail on the structure.

    Zaire -
    Thanks for the example.
    I was using alternate syntax so I could squirt unadulterated html in the crevices and started with example code from the afore mentioned website:

    <?switch ( $test ) :?>
    <?case 'foo' :?>
    <FONT face="verdana">Foo</FONT>
    <?break ;?>
    <?case 'bar' :?>
    <FONT face="verdana">Bar</FONT>
    <?default :?>
    <?break ;?>
    <FONT face="verdana">Default</FONT>
    <?endswitch ;?>

    I finally tore it apart and used if..., elseif..., endif... for the outer control.

    Today, I tried to recreate the error and for the life of me, I can't!

    It had to have been a goofy syntax error on my part.

    Still, it would be nice to have documentation on the many php error messages and I was unable to find anything comprehensive.

    Thanks guys!

  5. #5
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use alot of commenting when I develope, and go step by step so I can find my error's fast

    -Eric
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •