Learn Codeigniter

Include File In PHP

We can use code of one PHP file into another PHP file,with the include or require statement.

Syntax

include 'fileName'; or require 'fileName';

Example

<?php
 include 'sidebar.php';// We can include it multiple times.
?>
<?php
 require 'sidebar.php'; // We can include it multiple times.
?>
<?php
 include_once 'sidebar.php'; // We can include it Once only.
?>
<?php
 $path = $_SERVER['DOCUMENT_ROOT'];
 $path .= "/frontend/sidebar.php";
 include_once($path);
?>

Note: When we included a file with the include statement and PHP cannot find it, the script produce a warning (E_WARNING) and the script will continue. When we included a file with the require statement and PHP cannot find it, the script produce a fatal error (E_COMPILE_ERROR) and stop the script.