You can add PHP file contents to another PHP file before the server does. There are two PHP functions that can be used to insert one PHP file into another PHP file.

  • The include() Function
  • The require() Function

The include() Function

The include() function takes all the text from the specified file and copies it to a file using the included function. If there is a problem loading the file then the include() function generates warning but the script will continue to perform.

Now create as many pages as you like and add this file to create a header. For example now your test.php file may contain the following content.

#test.php

<html>
   <body>
      <?php include("some_page.php"); ?>
      <p>This is an example to show how to include PHP file!</p>
   </body>
</html>

output:

Some more contet from some_page.php this is external file and including by include()
This is an example to show how to include PHP file!

The require() Function

The require () function takes all the text of the specified file and copies it to a file using the included function. If there is a problem loading the file where the require () function generates a fatal error and stops the script execution.

<html>
   <body>
       
       <?php require("this_page_is_not_availble.php"); ?>
       <p>This is an example to show how to include wrong PHP file!</p>
   
   </body>
</html>

This time file execution halts and nothing is displayed.