How to enclose static and dynamic PHP code?

It is common practice websites consist of hundreds of pages which all dispose of the same header, sidebar or footer. In that case creating a separate PHP file containing your headers, footers and sidebars and enclosing them in a main file might be a very smart step to take.  When considering a future change one will only need to change a single file instead of verifying and changing all website pages. We could of course think of various other options like enclosing a PHP script or enclosing HTML in a dynamic script.

Enclosing a code can be realized by means of a static or dynamic code. But what’s the exact difference between the both of them? Enclosing a static code will be executed during the script enclosure and the PHP-parser has already processed the script. During dynamic code enclosure the code will be enclosed at an earlier phase, namely during the PHP-parser script processing when the script hasn’t been executed yet. Static code enclosure can be realized by means of include or include_once while dynamic code enclosure can be realized by means of require or require_once. The difference between include and include_once is pretty straightforward. Include means the code can be enclosed multiple times while include_once only allows a single enclosure. The same applies to require/require_once, with one difference, namely that the code isn’t only enclosed but also processed. Important to be aware of is the fact include and include_once are mainly used to enclose static HTML code while require and require_once are mainly used for the execution of other codes like for example a web application.



Was this useful?