Jump to content

Nested Php Commands


iwato
 Share

Recommended Posts

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

"<?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

"<?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

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...