SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Switch Statement Alternative Syntax

    Is it possible to use the same type of alternative syntax with a switch statement as the if statement below?

    Code:
    <?php if ($a == b): ?>
      <p>Most of your base are belong to us</p>
    <?php endif; ?>

  2. #2

  3. #3
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Took me a bit to get the syntax correct, but is working now:


    Code:
    <?php switch($v): 
       case "addmember": ?>
          <p>A big HTML form went here</p>
      <?php break; ?>
      <?php case "addcat": ?>  
          <p>A different HTML form went here</p>    
      <?php break; ?>
    ...... and a few more cases went here...
    <?php endswitch; ?>
    I've seen some post suggesting NOT to use this format, but I'm not sure why. If there's only a small bit of stuff going on for each case, I'd use the regular syntax, but for the page in question, it seems MUCH more readable and easier to manage to me. I can't see having to "phpify" several 20+ input forms inside of case{bunch of php stuff here}!

    Maybe it's just having Coldfusion as my first language and I'm trying to conform PHP to what I'm used to. Thoughts?

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,112
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Consider this as a different approach (still readable and maintainable -- each form has its own html template file that gets included)
    PHP Code:
    <?php 
    switch($v)
    {
       case 
    "addmember":
          include(
    'addmember.phtml');
          break;
       case 
    "addcat":
          include(
    'addcat.phtml');
          break;
    //...... and a few more cases went here...
    }
    ?>

  5. #5
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Consider this as a different approach (still readable and maintainable -
    That's an good idea as well ... thanks!


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
  •