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

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

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:
header.php

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

menu.php

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

footer.php

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

Page1.php

<?Php
include('header.php');
include('menu.php');
?>
<div class="content"> Here is the content of page 1 </ div>
 
<?Php
include('footer.php');
?>

page2.php

<?Php
include('header.php');
include('menu.php');
?>
<div class = "content"> Here is the content of page 2 </ div>
 
<?Php
include('footer.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.

Here is an example:

site1.inc.php:

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

page2.php

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

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.

About The Author

Related posts

Leave a Reply