Close code blocks in PHP sections?
Posted: Mon Mar 09, 2009 8:07 am
Within each section of PHP in a given page -- that is, between the <?php and ?> -- is it necessary to close all PHP control structures? In other words, can you have an if, foreach, etc. with an opening brace, end that PHP block with ?>, then further down the page start another <?php block and have the closing brace in that block?
If so, it seems a bit weird to me (and confusing) to program that way. Any HTML intervening between the PHP sections could of course not have any variable substitution, but I suppose there could be cases where this would be useful. If I have chunks of HTML to put out within a control structure, I usually use a print with HEREDOC syntax.
Another point of confusion is that if the interrupted control structure is an "if", the HTML would be emitted regardless of the conditional situation, e.g.:
<?php>
if (false) {
?>
<p>This is output regardless of the if condition</p>
<?php
// other code
}
?>
Am I correct in all of this?
Doug G
If so, it seems a bit weird to me (and confusing) to program that way. Any HTML intervening between the PHP sections could of course not have any variable substitution, but I suppose there could be cases where this would be useful. If I have chunks of HTML to put out within a control structure, I usually use a print with HEREDOC syntax.
Another point of confusion is that if the interrupted control structure is an "if", the HTML would be emitted regardless of the conditional situation, e.g.:
<?php>
if (false) {
?>
<p>This is output regardless of the if condition</p>
<?php
// other code
}
?>
Am I correct in all of this?
Doug G