iwato Posted February 27, 2010 Share Posted February 27, 2010 I know that the SWITCH command is probably not recommended in PHP for the same reason that it is not recommended in Javascript; nevertheless, by working with the SWITCH command I can resolve a more fundamental problem that I am having -- namely, nested PHP commands. So, please bear with me.How does one rewrite the following code so that it works?<?PHP switch ($variable) { case 'A' : <p>blah, blah, blah</p> <?PHP doTheA_Thing; ?>; break; case 'B' : <p>blah, blah, blah</p>; <?PHP doTheB_Thing; ?>; break; case 'C' : <p>blah, blah, blah</p>; <?PHP doTheCThing; ?>; break; default: <p>blah, blah, blah</p>; <?PHP doTheA_Thing; ?>; }?> Roddy Link to comment Share on other sites More sharing options...
boen_robot Posted February 27, 2010 Share Posted February 27, 2010 "<?php" sections can't (and don't need to) be nested. You can end and restart them though, like for example: <?phpswitch ($variable) {case 'A' :?><p>blah, blah, blah</p><?php doTheA_Thing;break;case 'B' :?><p>blah, blah, blah</p>;<?php doTheB_Thing;break;case 'C' :?><p>blah, blah, blah</p>;<?php doTheCThing;break;default:?><p>blah, blah, blah</p>;<?php doTheA_Thing;}?> Link to comment Share on other sites More sharing options...
iwato Posted February 27, 2010 Author Share Posted February 27, 2010 "<?php" sections can't (and don't need to) be nested. You can end and restart them though, like for example:Blah, blah, blah This was extremely helpful and easily applied. That PHP code can be stopped and restarted is both neat and cumbersome: neat, because it is easy to apply; and cumbersome, because it is difficult to read. In any case, this appears to be the nature of the beast, and I must make do.Many thanks. I am now off to a much better start.Roddy Link to comment Share on other sites More sharing options...
Synook Posted February 28, 2010 Share Posted February 28, 2010 If you don't like it, there are other structures you can use, such as HEREDOC. Link to comment Share on other sites More sharing options...
iwato Posted March 1, 2010 Author Share Posted March 1, 2010 If you don't like it, there are other structures you can use, such as HEREDOC.Thanks, I have just looked at HEREDOC, but it does not solve the problem of quick visual recognition of nested-like PHP constructs similar to the example provided by boen_robot that I was able to make work, by the way: The GENA Project Link to comment Share on other sites More sharing options...
Synook Posted March 1, 2010 Share Posted March 1, 2010 I meant, if you don't like closing and opening PHP tags over and over again, but still need to print out large amounts of HTML. For small amounts, you can just use echo. Link to comment Share on other sites More sharing options...
iwato Posted March 1, 2010 Author Share Posted March 1, 2010 I meant, if you don't like closing and opening PHP tags over and over again, but still need to print out large amounts of HTML. For small amounts, you can just use echo.The time will surely come for both.Many thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.