[ACCEPTED]-PHP: How to hide/display chunks of HTML-html
Considering PHP as an embedded language 6 you should, in order to get better readability, use 5 the specific language forms for the "templating".
Instead 4 of using echo you could just write the HTML 3 outside the <?php ?>
tags (never use <? ?>
which could 2 be misleading).
Also it is suggested to use 1 if () :
instead of if () {
, for example:
<body>
<?php if (true) : ?>
<p>It is true</p>
<?php endif; ?>
</body>
References:
You don't need to put the HTML into an echo
statement. Think 12 of the HTML as implicitly being echo
ed. So, for 11 conditionally showing a chunk of HTML, you'd 10 be looking for a construct like this:
<?php
if (condition == true) {
?>
<div>
<p>Some content</p>
</div>
<?php
}
?>
So 9 the HTML string literal that exists outside 8 of the PHP tags is just implicitly delivered 7 to the page, but you can wrap logic around 6 it to drive it. In the above, the div
and 5 its contents are all within the scope of 4 the if
statement, and so that chunk of HTML 3 (even though it's outside of the PHP tags) is 2 only delivered if the condition in the PHP 1 code is true
.
Try this:
<?php if ($var == true) { ?>
<table>...</table>
<?php } ?>
You can use PHP tags like wrapped 2 around HTML and based on the conditional 1 the HTML will either be rendered or it won't.
This should work too. E.g. for hiding/displaying 1 a table tag.
<table <?php echo (condition == true)?'visible':'hidden'; ?> ></table>
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.