ankitweblogic logo
W e b t u t o r i a l s

Define PHP

PHP Installation

PHP INI File

PHP Case Sensitivity

PHP Variable

PHP Echo & Print

PHP Operators

PHP Receiving Input

PHP Decision Making

PHP Loops

PHP Jumping Statement

PHP Image Gallery

PHP Arrays

PHP File Upload

PHP String Functions

PHP Math Functions

PHP Functions

PHP Variable Scope

PHP Constant Variable

PHP Superglobals

PHP Form Validation

PHP Include Statement

PHP Filter

PHP File Handling

PHP Date Functions

PHP Cookies

PHP Session

PHP Send Emails

PHP Captcha

PHP-MySQL Connectivity

PHP MySQL Select

PHP MySQL Insert

PHP MySQL Delete

PHP MySQL Update

PHP MySQL Injection

PHP include statement

Including files is very useful when you want to include the same text, HTML, or PHP file on multiple pages of a website. It is possible to insert the content of one PHP file into another PHP file with the include or require statement.

The include and require statements are identical, except:
require will produce a fatal error (E_COMPILE_ERROR) and stop the script.
include will only produce a warning (E_WARNING) and the script will continue.

Example 1: Assume we have a standard header file called "header.php"

<?php
   echo "<h1> Website Heading </h1>";
   <hr />
?>
To include the header file in a page, use the include statement:
<html>
  <body>
     <?php include 'header.php';?>
     <h1>Welcome to my home page!</h1>
     <p>Some text.</p>
     <p>Some more text.</p>
  </body>
</html>
Example 2: Assume we have a standard menu file called "menu.php":
<?php
  echo '<a href="home.php">Home</a> -
  <a href="html.php">HTML Tutorial</a> -
  <a href="css.php">CSS Tutorial</a> -
  <a href="js.php">JavaScript Tutorial</a> -
  <a href="php.php">PHP Tutorial</a>';
?>
All pages in the Web site should use this menu file. Here is how it can be done like this:
<html>
  <body>
    <div class="menu">
      <?php include 'menu.php';?>
    </div>
    
    <h1>Welcome to my home page!</h1>
    <p>Some text.</p>
    <p>Some more text.</p>
  
  </body>
</html>

PHP include vs. require

The require statement is also used to include a file into the PHP code. However, there is one big difference between include and require, when a file is included with the include statement and PHP cannot find it, the script will continue to execute:

Example:
<html>
  <body>
    <h1>Welcome to my home page! </h1>
    <?php require 'noFileExists.php'; ?>
  </body>
</html>