PHP Lesson 22 – Include other PHP files – include(), require()

In the previous tutorials, you learned the basics of using PHP. At the end of our introduction, here are some tips on how to structure your code clearly.

Split website into several files

PHP has some features that allow you to split your scripts into several files. This is very helpful in many cases. Suppose you have a homepage with dozens or hundreds of subpages. If you want to change an entry in the menu, you have to change all pages. This is of course quite expensive. Instead, it is advisable to define the various areas of your homepage (header, menu, footer) somewhere in a central file and later output this easily. A change to the menu would only have to be done in one file and would then be displayed everywhere. Fortunately, with PHP this is very easy.

PHP has the functions include($filename) and require($filename) which allow you to load code from another file. If the file is not found then include() only gives a warning, while require() stops the script. Which of the two functions you use is in most cases irrelevant.

To structure a homepage you can now use the following code:
header.php

To change the menu, we just have to adjust the menu.php and we have the new menu on all subpages.

Split complex PHP scripts into multiple files

Whether you have in the files you include by include / require now HTML code in it or if you have PHP code, it does not matter. If you have PHP code in it, you should keep in mind that the code will be “copied out” from the other file and then executed in the new script. This has advantages and disadvantages: You can easily define configuration variables that you use everywhere. Problem is if you use the same variables in both scripts, then they can overwrite each other.

As you will notice, page1.inc.php overrides the value of the $ username variable. This has unpleasant side effects in this case. So here are some tips on how to split larger scripts into multiple files.

includes at the beginning of the script – The includes and requires should always be at the very beginning of the script, so in the above case, require should precede the if statement. This prevents variables from being overwritten by the include. Excluded here are at most pages that only output HTML code, as in the example before the footer of the website.

Clear separation between files that are loaded by include and those that are called directly – you should clearly distinguish between files you want to load via include and those that the visitor wants to call directly. In order to make this clear, it is recommended to mark files which are loaded via include with the file extension .inc.php. Often we recommend a separate folder in which you save all your include files. But be aware, your visitors can also directly call your include files via browser if they adjust the URL accordingly.

Frequently and centrally outsource functions to your own files – functions and variables that you need frequently and in different places, you should outsource as a separate file and then load it via include. Typical examples are a config.inc.php to store central configuration values , a database_connection.inc.php to connect to the database and a functions.inc.php that contains your own written functions.