With the simple addition of PHP and the include statement, you can streamline this process dramatically, as well as get your feet wet with some dynamic website building.
Introducing PHP include
The include and require statements are available to insert useful codes written in other files, in the flow of execution.
PHP ‘include’ vs ‘include_once’ vs ‘require’ vs ‘require_once’
There are several ways to include outside files into your PHP script:
when the file is not required and application flow should continue when not found
optional dependencies that would produce errors on subsequent loading or maybe remote file inclusion that you do not want to happen twice due to the HTTP overhead
when the file is required by your application
when the file contains content that would produce an error on subsequent inclusion
We recommend require_once for most use-cases, as it will provide the most consistent expectation. Remember that if you do in fact want to load the file multiple times, just use ‘require’.
Setting up a simple PHP server-side include
Assuming you have PHP installed on your server already, all you need to do is make a page file, say index.php, and include this code:
After you have that setup, let’s create the header.php file:
And the footer.php file:
And that’s it!
Now all you need to do is setup those three files on your server and try them out! If you have any questions or comments, feel free to ask below.
Also, if anyone one would like additional features added to the tutorial, please let us know!