Press enter to see results or esc to cancel.

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

Share this
PHP Lesson 22 - Include other PHP files - include(), require() - PHP Lesson 22 - Include other PHP files - include(), require() - PHP Lesson 22 - Include other PHP files - include(), require() - php include require
php 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:

<!doctype html>
    <meta charset="utf-8">
    <title>Main Website</title>
      .content {border: 1px solid #000000;}


<li> <a href="page1.php"> page 1 </a></ li>
<li> <a href="page2.php"> page 2 </a></ li>
</ Ul>


Footer: <a href="impressum.php">Impressum</a> | <a href="contact.php">Contact</a>


<div class="content"> Here is the content of page 1 </ div>


<div class = "content"> Here is the content of page 2 </ div>

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.

Here is an example:

$config = array("min_password_length" => 8, "max_password_length" => 16);
$username = "Coding180";


if (isset ($_GET ['username'])) {
   $username = $_GET['username'];
} else {
   $username = "guest";
require( '');
echo "Hello $username, your password should be between".$config['min_password_length']. "and". $config['max_password_length']. "have characters";

As you will notice, 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 to store central configuration values , a to connect to the database and a that contains your own written functions.

How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this