PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Include And Require: File Include Statement

The include and require statements are used to insert the contents of one PHP file into another PHP file before the server executes it. They are useful for reusing PHP code across multiple pages, which can be a significant time saver.

Here's a basic usage of each:

include 'filename.php';
require 'filename.php';

The difference between them is how they handle errors:

  • include will emit a warning (E_WARNING) and the script will continue if the file is not found.
  • require will emit a fatal error (E_COMPILE_ERROR) and stop the script if the file is not found.

There are also include_once and require_once, which are similar to include and require, respectively, but if the code from a file has already been included, it will not be included again.

Here's a basic example of how you might use these statements. Imagine you have a file named header.php that contains the HTML header of your page, and you want to include it in all your pages.

header.php:

<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
</head>
<body>

index.php:

<?php include 'header.php'; ?>
    <h1>Welcome to my website!</h1>
</body>
</html>

In the above example, index.php includes header.php. When you access index.php, it will also contain the content from header.php. This is useful for elements like headers, footers, or reusable components that appear on multiple pages.

Remember, when using include or require, the path specified is relative to the file where the include or require statement is being used. If the file is not found at the specified path, PHP will also check in the directories specified in the include_path configuration setting in the php.ini file. If the file is not found there either, include will emit a warning and require will emit a fatal error.

  1. How to use include and require in PHP:

    • Use include and require statements to include external files.
    <?php
    // Using include
    include 'header.php';
    
    // Using require
    require 'footer.php';
    
  2. Including external files with include in PHP:

    • Include external files in your PHP script using include.
    <?php
    include 'functions.php';
    // Rest of the code
    
  3. Requiring essential files using require in PHP:

    • Require essential files using the require statement.
    <?php
    require 'config.php';
    // Rest of the code
    
  4. Differences between include and require in PHP:

    • Understand the differences between include and require.
    <?php
    // Using include
    include 'file1.php'; // If file not found, script continues with a warning
    
    // Using require
    require 'file2.php'; // If file not found, script terminates with a fatal error
    
  5. Using include_once and require_once in PHP:

    • Use include_once and require_once to include files only once.
    <?php
    include_once 'config.php'; // Include only once
    require_once 'functions.php'; // Require only once
    // Rest of the code
    
  6. Dynamic file inclusion with variables in PHP:

    • Dynamically include files using variables.
    <?php
    $page = 'about';
    include $page . '.php'; // Includes about.php dynamically
    
  7. Error handling with include and require statements in PHP:

    • Handle errors when including files.
    <?php
    // Using include with error handling
    if (file_exists('header.php')) {
        include 'header.php';
    } else {
        die('Header file not found!');
    }
    
    // Using require with error handling
    if (file_exists('footer.php')) {
        require 'footer.php';
    } else {
        die('Footer file not found!');
    }