PHP include() | Call Another PHP File to Execute

The PHP include() function is used when we need to include an external PHP file in a PHP script, to execute the script available in that external file, in the current PHP file. For example:

   echo "Hey, PHP is Fun!<BR>";
   echo "Is not it?";

The file header.php available in the current directory, and contains the following script:

   echo "-------Welcome to<HR>";

Therefore the output of above example on include() function in PHP, is shown in the snapshot given below:

php include function

Now the question is, what if the included file does not exists?
Let us find out the answer, using the example given below:

   echo "Hey, PHP is Fun!<BR>";
   echo "Is not it?";

Now the output is:

php include function example

That is, since the file unknown.php is does not exists, therefore you are seeing the error. But the current script after the include() function still has executed. Use error_reporting(0); to turn off error reporting.

PHP include() Syntax

The syntax of include() function in PHP, is:

include "file";


include 'file';





Note - The include() function produce warning (E_WARNING) message, in case if the specified file does not exists, and the script continues its execution.

Why to Use include() in PHP?

I do not know, what is your purpose of using the function include(). But, with the use of include() function, the same script need not to be written for every file. That is, if some PHP script like header and footer content are available in multiple files of your web application. Then better to go with include() function to write the header and footer content at once, in two PHP file. And include these two files, in each, to avoid writing multiple times.

Another benefit is, if you want to change the content of footer and/or header, then just change the content of that two files, it reflects the change in whole application or all files, where these files are included. For example:

<?php error_reporting(0); ?>
<!DOCTYPE html>
      * {box-sizing: border-box;}
      body {margin: 0;}
      .head {padding: 22px 0; font-size: 1.4em; text-align: center; background: maroon; color: white;}
      .navigation {display: flex; background-color: peru;}
      .navigation a {color: white; padding: 12px 16px; text-decoration: none; text-align: center;}
      .cont {display: flex; flex-wrap: wrap; min-height: 540px;}
      .menu {flex: 20%; background-color: #ccc; padding: 16px;}
      .content {flex: 60%; padding: 16px;}
      .right {flex: 20%; background-color: grey; color: white; padding: 16px;}
      .foot {padding: 22px 0; text-align: center; background: maroon; color: white;}
      @media screen and (max-width: 600px) {.cont, .navigation {flex-direction: column;}}

   <?php include 'header.php'; ?>
   <div class="cont">
      <div class="menu">
         <p>----Some Menu----</p>
      <div class="content">
         <p>Content of the Page...</p>
      <div class="right">
         <p>----Right Side----</p>
   <?php include 'footer.php'; ?>


The content of the header.php file is:

<div class="head">CodesCracker</div>
   <div class="navigation">
      <a href="#">MenuOne</a>
      <a href="#">MenuTwo</a>
      <a href="#">MenuThree</a>
      <a href="#">MenuFour</a>
      <a href="#">MenuFive</a>

And the content of the footer.php file is:

<div class="foot">
   <p>Some texts/links...</p>

Now the output should be:

php include function file

PHP Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube