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; ?>
| SitePoint Sponsor |
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; ?>


Yes you can, your ending syntax would be endswitch;
Took me a bit to get the syntax correct, but is working now:
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}!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; ?>
Maybe it's just having Coldfusion as my first language and I'm trying to conform PHP to what I'm used to. Thoughts?


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...
}
?>
Bookmarks