Php: How To Hide/display Chunks Of Html
Solution 1:
Considering PHP as an embedded language you should, in order to get better readability, use the specific language forms for the "templating".
Instead of using echo you could just write the HTML outside the <?php ?>
tags (never use <? ?>
which could be misleading).
Also it is suggested to use if () :
instead of if () {
, for example:
<body><?phpif (true) : ?><p>It is true</p><?phpendif; ?></body>
References:
Solution 2:
You don't need to put the HTML into an echo
statement. Think of the HTML as implicitly being echo
ed. So, for conditionally showing a chunk of HTML, you'd be looking for a construct like this:
<?phpif (condition == true) {
?><div><p>Some content</p></div><?php
}
?>
So the HTML string literal that exists outside of the PHP tags is just implicitly delivered to the page, but you can wrap logic around it to drive it. In the above, the div
and its contents are all within the scope of the if
statement, and so that chunk of HTML (even though it's outside of the PHP tags) is only delivered if the condition in the PHP code is true
.
Solution 3:
Try this:
<?phpif ($var == true) { ?><table>...</table><?php } ?>
You can use PHP tags like wrapped around HTML and based on the conditional the HTML will either be rendered or it won't.
Solution 4:
This should work too. E.g. for hiding/displaying a table tag.
<table <?phpecho (condition == true)?'visible':'hidden'; ?> ></table>
Post a Comment for "Php: How To Hide/display Chunks Of Html"