A PHP Tip for Site Developers; Hiding a Section of a Web Page
This tip is intended to make things a bit easier for you, a bit faster, and a lot less stressful. It's really handy.
An entire section of a web page, of any size, can be hidden from the browser (and from source code viewers).
Of course, being a PHP tip, it can be applied only to PHP web pages.
PHP code on a web page runs before the browser receives the page. (Unlike JavaScript, which runs after the browser receives the page.) Therefore, the effect of this tip occurs before the browser ever sees the content.
Use this tip instead of commenting out a section of the web page with an HTML comment tag. Here are 3 reasons:
-
HTML comments can be seen when the web page source code is viewed.
-
Sections of a web page removed with an HTML comment tag that itself contains HTML comments can be become a confused mess. HTML comments don't nest within HTML comments, so the inner comment tags need to be disabled.
-
Using PHP to hide a web page section removes the section before it's sent to the browser. The section isn't there when the browser is used to view the source code.
Here's the code:
Immediately before the section to be hidden, insert this PHP code:
<?php if(false): ?>
Immediately after the section to be hidden, insert this PHP code:
<?php endif; ?>
The entire section between the two PHP items will be removed from the page. (The colon character [":"] in the first PHP item and the semi-colon character [";"] in the second PHP item are supposed to be that way.)
To temporarily (or permanently) display the content between the two PHP items, change if(false) to if(true) in the first PHP item.
You see how easy it is to switch back and forth, hiding and displaying a web page section, during development and testing.
I hope this tip is useful for you. We use it a lot. Not only for development, but also to hide comments within web pages so we don't need to keep a separate file of notes.
Will Bontrager